-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added the json() shortcut to the controller chapter #6502
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
Changes from 5 commits
9cda506
5ffa35c
3697884
8c53be7
8645cdd
8e7f397
ee1e665
4b0ba22
7bc2ff8
0bed9f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -762,9 +762,15 @@ headers and content that's sent back to the client:: | |
// create a simple Response with a 200 status code (the default) | ||
$response = new Response('Hello '.$name, Response::HTTP_OK); | ||
|
||
// create a JSON-response with a 200 status code | ||
$response = new Response(json_encode(array('name' => $name))); | ||
$response->headers->set('Content-Type', 'application/json'); | ||
// create a CSS-response with a 200 status code | ||
$response = new Response('<style> ... </style>'); | ||
$response->headers->set('Content-Type', 'text/css'); | ||
|
||
.. seealso:: | ||
|
||
Now that you know the basics you can continue your research on Symfony | ||
``Request`` and ``Response`` object in the | ||
:ref:`HttpFoundation component documentation <component-http-foundation-request>`. | ||
|
||
There are also special classes to make certain kinds of responses easier: | ||
|
||
|
@@ -778,11 +784,25 @@ There are also special classes to make certain kinds of responses easier: | |
:class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`. | ||
See :ref:`streaming-response`. | ||
|
||
.. seealso:: | ||
JSON helper | ||
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. Helper |
||
~~~~~~~~~~~ | ||
|
||
Now that you know the basics you can continue your research on Symfony | ||
``Request`` and ``Response`` object in the | ||
:ref:`HttpFoundation component documentation <component-http-foundation-request>`. | ||
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 |
||
|
||
// ... | ||
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()); | ||
|
||
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.
If we wanted to add this, the change should be done in the
2.3
branch.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.
This doc already existed in the article. I moved it wrongly. Reverted.