@@ -503,22 +503,6 @@ The Symfony templating engine is explained in great detail in the
503
503
.. index ::
504
504
single: Controller; Accessing services
505
505
506
- Sending JSON responses
507
- ~~~~~~~~~~~~~~~~~~~~~~
508
-
509
- If you're developing an API, you'll probably return JSON contents from your
510
- controllers. The ``json() `` method turns the given contents into JSON format and
511
- prepares the HTTP response accordingly for you::
512
-
513
- // returns '{"username":"jane.doe"}' and sets the proper Content-Type header
514
- $data = array('username' => 'jane.doe');
515
- return $this->json($data);
516
-
517
- The only required argument is the data to be sent, but ``json() `` defines three
518
- more optional arguments::
519
-
520
- $this->json($data, $status = 200, $headers = array(), $context = array());
521
-
522
506
.. _controller-accessing-services :
523
507
524
508
Accessing other Services
@@ -774,14 +758,17 @@ headers and content that's sent back to the client::
774
758
// create a simple Response with a 200 status code (the default)
775
759
$response = new Response('Hello '.$name, Response::HTTP_OK);
776
760
777
- // create a JSON -response with a 200 status code
778
- $response = new Response(json_encode(array('name' => $name)) );
779
- $response->headers->set('Content-Type', 'application/json ');
761
+ // create a CSS -response with a 200 status code
762
+ $response = new Response('<style> ... </style>' );
763
+ $response->headers->set('Content-Type', 'text/css ');
780
764
781
- There are also special classes to make certain kinds of responses easier:
765
+ .. seealso ::
766
+
767
+ Now that you know the basics you can continue your research on Symfony
768
+ ``Request `` and ``Response `` object in the
769
+ :ref: `HttpFoundation component documentation <component-http-foundation-request >`.
782
770
783
- * For JSON, there is :class: `Symfony\\ Component\\ HttpFoundation\\ JsonResponse `.
784
- See :ref: `component-http-foundation-json-response `.
771
+ There are also special classes to make certain kinds of responses easier:
785
772
786
773
* For files, there is :class: `Symfony\\ Component\\ HttpFoundation\\ BinaryFileResponse `.
787
774
See :ref: `component-http-foundation-serving-files `.
@@ -790,11 +777,27 @@ There are also special classes to make certain kinds of responses easier:
790
777
:class: `Symfony\\ Component\\ HttpFoundation\\ StreamedResponse `.
791
778
See :ref: `streaming-response `.
792
779
793
- .. seealso ::
780
+ Sending JSON responses
781
+ ~~~~~~~~~~~~~~~~~~~~~~
794
782
795
- Now that you know the basics you can continue your research on Symfony
796
- ``Request `` and ``Response `` object in the
797
- :ref: `HttpFoundation component documentation <component-http-foundation-request >`.
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
+
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
+ The only required argument is the data to be sent, but ``json() `` defines three
792
+ more optional arguments::
793
+
794
+ $this->json($data, $status = 200, $headers = array(), $context = array());
795
+
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 `.
798
801
799
802
Creating Static Pages
800
803
---------------------
0 commit comments