From 42d647d7674c3ba8efb0719e059b0c1c8fa20749 Mon Sep 17 00:00:00 2001 From: Dennis Fridrich Date: Sun, 10 Apr 2016 21:37:38 +0200 Subject: [PATCH 1/2] Added docs for file helper --- book/controller.rst | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/book/controller.rst b/book/controller.rst index f1b65ae09aa..78b95b11369 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -806,6 +806,43 @@ If the :doc:`serializer service ` is enabled in your application, contents passed to ``json()`` are encoded with it. Otherwise, the :phpfunction:`json_encode` function is used. +File helper +~~~~~~~~~~~ + +If you want to serve file use the +:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::file` +helper:: + + $this->file($file, $fileName = null, $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT); + +You can pass: + +* An instance of + :class:`Symfony\\Component\\HttpFoundation\\File` + in ``$file`` parameter (you can customize ``$fileName`` and ``$disposition``) + +* Path to file in ``$file`` parameter + +Example usage:: + + use Symfony\Component\HttpFoundation\File\File; + use Symfony\Component\HttpFoundation\ResponseHeaderBag; + + // This will send file with original name as attachment to browser + public function fileAction() + { + // Load file from file system + $file = new File('some_file.pdf'); + + return $this->file($file); + } + + // Server file from specified path + public function pathFileAction() + { + return $this->file('/path/to/my/picture.jpg'); + } + .. seealso:: Now that you know the basics you can continue your research on Symfony From 643c0d4ab081d20b6940a8014d96c287cf3a9ba9 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Fri, 8 Jul 2016 14:07:47 +0200 Subject: [PATCH 2/2] Tweak section to remove some text --- book/controller.rst | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/book/controller.rst b/book/controller.rst index 78b95b11369..c04d28fde87 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -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 `. + JSON Helper ~~~~~~~~~~~ @@ -809,46 +815,39 @@ the :phpfunction:`json_encode` function is used. File helper ~~~~~~~~~~~ -If you want to serve file use the -:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::file` -helper:: - - $this->file($file, $fileName = null, $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT); +.. versionadded:: 3.2 + The ``file()`` helper was introduced in Symfony 3.2. -You can pass: +You can use :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::file` +to serve a file from inside a controller:: -* An instance of - :class:`Symfony\\Component\\HttpFoundation\\File` - in ``$file`` parameter (you can customize ``$fileName`` and ``$disposition``) + $this->file($file, $fileName = null, $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT); -* Path to file in ``$file`` parameter +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. -Example usage:: +.. code-block:: php use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\ResponseHeaderBag; - // This will send file with original name as attachment to browser public function fileAction() { - // Load file from file system + // load file from the file system $file = new File('some_file.pdf'); - return $this->file($file); + // send the file as attachment to browser + return $this->file($file, 'custom_name.pdf'); } - // Server file from specified path public function pathFileAction() { + // serve file from specified path return $this->file('/path/to/my/picture.jpg'); } -.. seealso:: - - Now that you know the basics you can continue your research on Symfony - ``Request`` and ``Response`` object in the - :ref:`HttpFoundation component documentation `. - Creating Static Pages ---------------------