-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added the json() shortcut to the controller chapter #6502
Changes from 1 commit
9cda506
5ffa35c
3697884
8c53be7
8645cdd
8e7f397
ee1e665
4b0ba22
7bc2ff8
0bed9f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -774,35 +774,36 @@ headers and content that's sent back to the client:: | |
|
||
There are also special classes to make certain kinds of responses easier: | ||
|
||
* For JSON, there is :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`. | ||
See :ref:`component-http-foundation-json-response`. | ||
|
||
* For files, there is :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`. | ||
See :ref:`component-http-foundation-serving-files`. | ||
|
||
* For streamed responses, there is | ||
:class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`. | ||
See :ref:`streaming-response`. | ||
|
||
Sending JSON responses | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
JSON helper | ||
~~~~~~~~~~~ | ||
|
||
If you're developing an API, you'll probably return JSON contents from your | ||
controllers. The ``json()`` method turns the given contents into JSON format and | ||
prepares the HTTP response headers accordingly:: | ||
Returning JSON contents is increasingly popular for API-based applications. For | ||
that reason, the base controller class defines a ``json()`` method which creates | ||
a ``JsonResponse()`` and encodes the given contents automatically:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
|
||
// returns '{"username":"jane.doe"}' and sets the proper Content-Type header | ||
$data = array('username' => 'jane.doe'); | ||
return $this->json($data); | ||
// ... | ||
public function indexAction() | ||
{ | ||
// returns '{"username":"jane.doe"}' and sets the proper Content-Type header | ||
$data = array('username' => 'jane.doe'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imho we can omit the variable and pass the array directly to the method |
||
return $this->json($data); | ||
} | ||
|
||
The only required argument is the data to be sent, but ``json()`` defines three | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would omit this paragraph. Knowing that the helper method exist is sufficient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've removed the paragraph ... but updated the code because otherwise |
||
more optional arguments:: | ||
|
||
$this->json($data, $status = 200, $headers = array(), $context = array()); | ||
|
||
.. note:: | ||
|
||
The ``json()`` shortcut uses the :class:`Symfony\\Component\\HttpFoundation\\JsonResponse` | ||
class to create the response. If you prefer it, you can also use that class. | ||
See :ref:`component-http-foundation-json-response`. | ||
|
||
Creating Static Pages | ||
--------------------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Helper