Skip to content

Commit 786d8e0

Browse files
committed
Document the file() controller helper
1 parent d288ae5 commit 786d8e0

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

book/controller.rst

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,12 @@ There are also special classes to make certain kinds of responses easier:
782782
:class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`.
783783
See :ref:`streaming-response`.
784784

785+
.. seealso::
786+
787+
Now that you know the basics you can continue your research on Symfony
788+
``Request`` and ``Response`` object in the
789+
:ref:`HttpFoundation component documentation <component-http-foundation-request>`.
790+
785791
JSON Helper
786792
~~~~~~~~~~~
787793

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

809-
.. seealso::
815+
File helper
816+
~~~~~~~~~~~
810817

811-
Now that you know the basics you can continue your research on Symfony
812-
``Request`` and ``Response`` object in the
813-
:ref:`HttpFoundation component documentation <component-http-foundation-request>`.
818+
.. versionadded:: 3.2
819+
The ``file()`` helper was introduced in Symfony 3.2.
820+
821+
You can use the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::file`
822+
helper to serve a file from inside a controller::
823+
824+
public function fileAction()
825+
{
826+
// send the file contents and force the browser to download it
827+
return $this->file('/path/to/some_file.pdf');
828+
}
829+
830+
The ``file()`` helper provides some arguments to configure its behavior::
831+
832+
use Symfony\Component\HttpFoundation\File\File;
833+
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
834+
835+
public function fileAction()
836+
{
837+
// load the file from the filesystem
838+
$file = new File('/path/to/some_file.pdf');
839+
return $this->file($file);
840+
841+
// rename the downloaded file
842+
return $this->file($file, 'custom_name.pdf');
843+
844+
// display the file contents in the browser instead of downloading it
845+
$this->file('invoice_3241.pdf', 'my_invoice.pdf', ResponseHeaderBag::DISPOSITION_INLINE);
846+
}
814847

815848
Creating Static Pages
816849
---------------------

0 commit comments

Comments
 (0)