Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Document controller file() helper#6732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
wouterj wants to merge2 commits intomasterfromdfridrich-file_helper
Closed
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletionsbook/controller.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -782,6 +782,12 @@ There are also special classes to make certain kinds of responses easier:
:class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`.
See :ref:`streaming-response`.

.. seealso::

Now that you know the basics you can continue your research on Symfony
``Request`` and ``Response`` object in the
:ref:`HttpFoundation component documentation <component-http-foundation-request>`.

JSON Helper
~~~~~~~~~~~

Expand All@@ -806,11 +812,41 @@ If the :doc:`serializer service </cookbook/serializer>` is enabled in your
application, contents passed to ``json()`` are encoded with it. Otherwise,
the :phpfunction:`json_encode` function is used.

.. seealso::
File helper
~~~~~~~~~~~

Now that you know the basics you can continue your research on Symfony
``Request`` and ``Response`` object in the
:ref:`HttpFoundation component documentation <component-http-foundation-request>`.
.. versionadded:: 3.2
The ``file()`` helper was introduced in Symfony 3.2.

You can use :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::file`
to serve a file from inside a controller::

$this->file($file, $fileName = null, $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT);

You can pass an :phpclass:`SplFileInfo` instance (like
:class:`Symfony\\Component\\HttpFoundation\\File`) or the path to a file as
first argument. Using the second and third arguments, you can customize the
send filename and the disposition.

.. code-block:: php

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

public function fileAction()
{
// load file from the file system
$file = new File('some_file.pdf');

// send the file as attachment to browser
return $this->file($file, 'custom_name.pdf');
}

public function pathFileAction()
{
// serve file from specified path
return $this->file('/path/to/my/picture.jpg');
}

Creating Static Pages
---------------------
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp