Skip to content
This repository

Add documentation for the new BinaryFileResponse class (#1866) #2416

Merged
merged 3 commits into from about 1 year ago

4 participants

John Bafford Ryan Weaver Wouter J Michaël Perrin
John Bafford
Q A
Doc fix? yes
New docs? yes (symfony/symfony#4546)
Applies to 2.2
Fixed tickets #1866
book/controller.rst
@@ -726,8 +726,11 @@ headers and content that's sent back to the client::
726 726
 
727 727
 .. tip::
728 728
 
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
+    - For JSON, there is :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`.
1
Wouter J Collaborator
WouterJ added a note March 30, 2013

please add an empty line before this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
components/http_foundation/introduction.rst
@@ -406,13 +406,15 @@ represented by a PHP callable instead of a string::
406 406
     });
407 407
     $response->send();
408 408
 
409  
-Downloading Files
  409
+.. _component-http-foundation-serving-files:
  410
+
  411
+Serving Files
410 412
 ~~~~~~~~~~~~~~~~~
1
Wouter J Collaborator
WouterJ added a note March 30, 2013

a couple ~ to much here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
components/http_foundation/introduction.rst
@@ -424,6 +426,26 @@ abstracts the hard work behind a simple API::
424 426
 
425 427
     $response->headers->set('Content-Disposition', $d);
426 428
 
  429
+.. versionadded:: 2.2
  430
+    The :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse` class was added in Symfony 2.2.
1
Wouter J Collaborator
WouterJ added a note March 30, 2013

You should break the line after the first word crossing the 72 character

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
components/http_foundation/introduction.rst
@@ -424,6 +426,26 @@ abstracts the hard work behind a simple API::
424 426
 
425 427
     $response->headers->set('Content-Disposition', $d);
426 428
 
  429
+.. versionadded:: 2.2
  430
+    The :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse` class was added in Symfony 2.2.
  431
+
  432
+Alternatively, if you are serving a static file, you can use a
  433
+:class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`::
  434
+
  435
+    use Symfony\Component\HttpFoundation\BinaryFileResponse
  436
+    
  437
+    $file = 'path/to/file.txt';
  438
+    $response = new BinaryFileResponse($file);
  439
+
  440
+The :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse` will
1
Wouter J Collaborator
WouterJ added a note March 30, 2013

There is no need to add 2 API links to the same page. I prefer to use the normal BinaryFileResponse inline code block here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
book/controller.rst
@@ -726,8 +726,12 @@ headers and content that's sent back to the client::
726 726
 
727 727
 .. tip::
728 728
 
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\\JsonResponse`.
2

A little typo here: I think you meant Symfony\\Component\\HttpFoundation\\BinaryFileResponse

John Bafford
jbafford added a note March 31, 2013

You're correct. I have that fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Ryan Weaver weaverryan merged commit 4140805 into from April 03, 2013
Ryan Weaver weaverryan closed this April 03, 2013
Ryan Weaver
Collaborator

Hey John!

This is really great - and I get to learn about a new feature from your work ;). I've only made a few small changes and also mentioned X-Sendfile at sha: 615a7b3. If you see any issues, let me know!

Thanks!

John Bafford

Glad to be of help! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
8  book/controller.rst
Source Rendered
@@ -726,8 +726,12 @@ headers and content that's sent back to the client::
726 726
 
727 727
 .. tip::
728 728
 
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
731 735
 
732 736
 .. index::
733 737
    single: Controller; Request object
31  components/http_foundation/introduction.rst
Source Rendered
@@ -406,13 +406,15 @@ represented by a PHP callable instead of a string::
406 406
     });
407 407
     $response->send();
408 408
 
409  
-Downloading Files
410  
-~~~~~~~~~~~~~~~~~
  409
+.. _component-http-foundation-serving-files:
  410
+
  411
+Serving Files
  412
+~~~~~~~~~~~~~
411 413
 
412 414
 .. versionadded:: 2.1
413 415
     The ``makeDisposition`` method was added in Symfony 2.1.
414 416
 
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
416 418
 response. While creating this header for basic file downloads is easy, using
417 419
 non-ASCII filenames is more involving. The
418 420
 :method:`Symfony\\Component\\HttpFoundation\\Response::makeDisposition`
@@ -424,6 +426,26 @@ abstracts the hard work behind a simple API::
424 426
 
425 427
     $response->headers->set('Content-Disposition', $d);
426 428
 
  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
+
427 449
 .. _component-http-foundation-json-response:
428 450
 
429 451
 Creating a JSON Response
@@ -442,7 +464,8 @@ right content and headers. A JSON response might look like this::
442 464
     $response->headers->set('Content-Type', 'application/json');
443 465
 
444 466
 .. 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.
446 469
 
447 470
 There is also a helpful :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`
448 471
 class, which can make this even easier::
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.