Skip to content

Commit 7a8ad76

Browse files
javiereguiluzwouterj
authored andcommitted
Moved the json() helper explanation
1 parent 97a2f9c commit 7a8ad76

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

book/controller.rst

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -503,22 +503,6 @@ The Symfony templating engine is explained in great detail in the
503503
.. index::
504504
single: Controller; Accessing services
505505

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-
522506
.. _controller-accessing-services:
523507

524508
Accessing other Services
@@ -774,14 +758,17 @@ headers and content that's sent back to the client::
774758
// create a simple Response with a 200 status code (the default)
775759
$response = new Response('Hello '.$name, Response::HTTP_OK);
776760

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');
780764

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>`.
782770

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:
785772

786773
* For files, there is :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`.
787774
See :ref:`component-http-foundation-serving-files`.
@@ -790,11 +777,27 @@ There are also special classes to make certain kinds of responses easier:
790777
:class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`.
791778
See :ref:`streaming-response`.
792779

793-
.. seealso::
780+
Sending JSON responses
781+
~~~~~~~~~~~~~~~~~~~~~~
794782

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`.
798801

799802
Creating Static Pages
800803
---------------------

0 commit comments

Comments
 (0)