@@ -770,35 +770,36 @@ headers and content that's sent back to the client::
770
770
771
771
There are also special classes to make certain kinds of responses easier:
772
772
773
+ * For JSON, there is :class: `Symfony\\ Component\\ HttpFoundation\\ JsonResponse `.
774
+ See :ref: `component-http-foundation-json-response `.
775
+
773
776
* For files, there is :class: `Symfony\\ Component\\ HttpFoundation\\ BinaryFileResponse `.
774
777
See :ref: `component-http-foundation-serving-files `.
775
778
776
779
* For streamed responses, there is
777
780
:class: `Symfony\\ Component\\ HttpFoundation\\ StreamedResponse `.
778
781
See :ref: `streaming-response `.
779
782
780
- Sending JSON responses
781
- ~~~~~~~~~~~~~~~~~~~~~~
783
+ JSON helper
784
+ ~~~~~~~~~~~
782
785
783
- If you're developing an API, you'll probably return JSON contents from your
784
- controllers. The ``json() `` method turns the given contents into JSON format and
785
- prepares the HTTP response headers accordingly ::
786
+ Returning JSON contents is increasingly popular for API-based applications. For
787
+ that reason, the base controller class defines a ``json() `` method which creates
788
+ a `` JsonResponse() `` and encodes the given contents automatically ::
786
789
787
- // returns '{"username":"jane.doe"}' and sets the proper Content-Type header
788
- $data = array('username' => 'jane.doe');
789
- return $this->json($data);
790
+ // ...
791
+ public function indexAction()
792
+ {
793
+ // returns '{"username":"jane.doe"}' and sets the proper Content-Type header
794
+ $data = array('username' => 'jane.doe');
795
+ return $this->json($data);
796
+ }
790
797
791
798
The only required argument is the data to be sent, but ``json() `` defines three
792
799
more optional arguments::
793
800
794
801
$this->json($data, $status = 200, $headers = array(), $context = array());
795
802
796
- .. note ::
797
-
798
- The ``json() `` shortcut uses the :class: `Symfony\\ Component\\ HttpFoundation\\ JsonResponse `
799
- class to create the response. If you prefer it, you can also use that class.
800
- See :ref: `component-http-foundation-json-response `.
801
-
802
803
Creating Static Pages
803
804
---------------------
804
805
0 commit comments