Skip to content

Commit 78aa9cb

Browse files
javiereguiluzwouterj
authored andcommitted
Simplified everything
1 parent 7a8ad76 commit 78aa9cb

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

book/controller.rst

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -770,35 +770,36 @@ headers and content that's sent back to the client::
770770

771771
There are also special classes to make certain kinds of responses easier:
772772

773+
* For JSON, there is :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`.
774+
See :ref:`component-http-foundation-json-response`.
775+
773776
* For files, there is :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`.
774777
See :ref:`component-http-foundation-serving-files`.
775778

776779
* For streamed responses, there is
777780
:class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`.
778781
See :ref:`streaming-response`.
779782

780-
Sending JSON responses
781-
~~~~~~~~~~~~~~~~~~~~~~
783+
JSON helper
784+
~~~~~~~~~~~
782785

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

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+
}
790797

791798
The only required argument is the data to be sent, but ``json()`` defines three
792799
more optional arguments::
793800

794801
$this->json($data, $status = 200, $headers = array(), $context = array());
795802

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-
802803
Creating Static Pages
803804
---------------------
804805

0 commit comments

Comments
 (0)