Skip to content

Commit 4140805

Browse files
committed
Merge pull request #2416 from jbafford/i1866
Add documentation for the new BinaryFileResponse class (#1866)
2 parents f17ba6c + 5345055 commit 4140805

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

book/controller.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,12 @@ headers and content that's sent back to the client::
726726

727727
.. tip::
728728

729-
There is also a special :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`
730-
class that helps return JSON responses. See :ref:`component-http-foundation-json-response`.
729+
There are also special classes to make certain kinds of responses easier:
730+
731+
- For JSON, there is :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`.
732+
See :ref:`component-http-foundation-json-response`.
733+
- For files, there is :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`.
734+
See :ref:`component-http-foundation-serving-files
731735

732736
.. index::
733737
single: Controller; Request object

components/http_foundation/introduction.rst

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,15 @@ represented by a PHP callable instead of a string::
406406
});
407407
$response->send();
408408

409-
Downloading Files
410-
~~~~~~~~~~~~~~~~~
409+
.. _component-http-foundation-serving-files:
410+
411+
Serving Files
412+
~~~~~~~~~~~~~
411413

412414
.. versionadded:: 2.1
413415
The ``makeDisposition`` method was added in Symfony 2.1.
414416

415-
When uploading a file, you must add a ``Content-Disposition`` header to your
417+
When sending a file, you must add a ``Content-Disposition`` header to your
416418
response. While creating this header for basic file downloads is easy, using
417419
non-ASCII filenames is more involving. The
418420
:method:`Symfony\\Component\\HttpFoundation\\Response::makeDisposition`
@@ -424,6 +426,26 @@ abstracts the hard work behind a simple API::
424426

425427
$response->headers->set('Content-Disposition', $d);
426428

429+
.. versionadded:: 2.2
430+
The :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`
431+
class was added in Symfony 2.2.
432+
433+
Alternatively, if you are serving a static file, you can use a
434+
:class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`::
435+
436+
use Symfony\Component\HttpFoundation\BinaryFileResponse
437+
438+
$file = 'path/to/file.txt';
439+
$response = new BinaryFileResponse($file);
440+
441+
The ``BinaryFileResponse`` will automatically handle ``Range`` and
442+
``If-Range`` headers from the request. You can also set the ``Content-Type``
443+
of the sent file, or change its ``Content-Disposition``::
444+
445+
$response->headers->set('Content-Type', 'text/plain')
446+
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'filename.txt');
447+
448+
427449
.. _component-http-foundation-json-response:
428450

429451
Creating a JSON Response
@@ -442,7 +464,8 @@ right content and headers. A JSON response might look like this::
442464
$response->headers->set('Content-Type', 'application/json');
443465

444466
.. versionadded:: 2.1
445-
The :class:`Symfony\\Component\\HttpFoundation\\JsonResponse` class was added in Symfony 2.1.
467+
The :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`
468+
class was added in Symfony 2.1.
446469

447470
There is also a helpful :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`
448471
class, which can make this even easier::

0 commit comments

Comments
 (0)