Skip to content
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

Closed
wants to merge 10 commits into from
Prev Previous commit
Next Next commit
Simplified everything
  • Loading branch information
javiereguiluz committed May 21, 2016
commit 8645cdd9bb65f4876a4503299685a5378623f996
29 changes: 15 additions & 14 deletions book/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the () should be removed here, it's not a method (nor an invokable class)


// 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');
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the paragraph ... but updated the code because otherwise json() looks "useless" (people may think that you can't change the status code, the headers, etc.)

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

Expand Down