From 9cda5069925dedf2ded90390bc4f0ae4719878fa Mon Sep 17 00:00:00 2001 From: Dennis Fridrich Date: Sun, 10 Apr 2016 21:38:43 +0200 Subject: [PATCH 01/10] Added docs for JSON helper --- book/controller.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/book/controller.rst b/book/controller.rst index c768b33407d..9995f20e2f2 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -778,6 +778,20 @@ There are also special classes to make certain kinds of responses easier: :class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`. See :ref:`streaming-response`. +JSON helper +~~~~~~~~~~~ + +You can simplify sending JSON response with +:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::json` +helper:: + + public function jsonAction() + { + $data = [1, 2, 3]; + + return $this->json($data); + } + .. seealso:: Now that you know the basics you can continue your research on Symfony From 5ffa35c412552f32703f4e76906d4547778bc16e Mon Sep 17 00:00:00 2001 From: Dennis Fridrich Date: Sun, 10 Apr 2016 22:07:58 +0200 Subject: [PATCH 02/10] Update docs for JSON helper --- book/controller.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/book/controller.rst b/book/controller.rst index 9995f20e2f2..ba2c1421033 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -785,6 +785,10 @@ You can simplify sending JSON response with :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::json` helper:: + $this->json($data, $status = 200, $headers = array(), $context = array()); + +For example if you want to send some simple array you can use it like this:: + public function jsonAction() { $data = [1, 2, 3]; From 3697884afe245e0d151cf09fa3cfdbb2208349d4 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 20 Apr 2016 15:10:26 +0200 Subject: [PATCH 03/10] Added the json() shortcut to the controller chapter --- book/controller.rst | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/book/controller.rst b/book/controller.rst index ba2c1421033..5bc78f94c2f 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -507,6 +507,22 @@ The Symfony templating engine is explained in great detail in the .. index:: single: Controller; Accessing services +Sending JSON responses +~~~~~~~~~~~~~~~~~~~~~~ + +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 accordingly for you:: + + // returns '{"username":"jane.doe"}' and sets the proper Content-Type header + $data = array('username' => 'jane.doe'); + return $this->json($data); + +The only required argument is the data to be sent, but ``json()`` defines three +more optional arguments:: + + $this->json($data, $status = 200, $headers = array(), $context = array()); + .. _controller-accessing-services: Accessing other Services @@ -778,24 +794,6 @@ There are also special classes to make certain kinds of responses easier: :class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`. See :ref:`streaming-response`. -JSON helper -~~~~~~~~~~~ - -You can simplify sending JSON response with -:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::json` -helper:: - - $this->json($data, $status = 200, $headers = array(), $context = array()); - -For example if you want to send some simple array you can use it like this:: - - public function jsonAction() - { - $data = [1, 2, 3]; - - return $this->json($data); - } - .. seealso:: Now that you know the basics you can continue your research on Symfony From 8c53be7b2e72d4cd0c050336ec11632289be3ff7 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 21 May 2016 11:18:23 +0200 Subject: [PATCH 04/10] Moved the json() helper explanation --- book/controller.rst | 55 ++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/book/controller.rst b/book/controller.rst index 5bc78f94c2f..c33f8864a84 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -507,22 +507,6 @@ The Symfony templating engine is explained in great detail in the .. index:: single: Controller; Accessing services -Sending JSON responses -~~~~~~~~~~~~~~~~~~~~~~ - -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 accordingly for you:: - - // returns '{"username":"jane.doe"}' and sets the proper Content-Type header - $data = array('username' => 'jane.doe'); - return $this->json($data); - -The only required argument is the data to be sent, but ``json()`` defines three -more optional arguments:: - - $this->json($data, $status = 200, $headers = array(), $context = array()); - .. _controller-accessing-services: Accessing other Services @@ -778,14 +762,17 @@ 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(''); + $response->headers->set('Content-Type', 'text/css'); -There are also special classes to make certain kinds of responses easier: +.. seealso:: + + Now that you know the basics you can continue your research on Symfony + ``Request`` and ``Response`` object in the + :ref:`HttpFoundation component documentation `. -* For JSON, there is :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`. - See :ref:`component-http-foundation-json-response`. +There are also special classes to make certain kinds of responses easier: * For files, there is :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`. See :ref:`component-http-foundation-serving-files`. @@ -794,11 +781,27 @@ There are also special classes to make certain kinds of responses easier: :class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`. See :ref:`streaming-response`. -.. seealso:: +Sending JSON responses +~~~~~~~~~~~~~~~~~~~~~~ - Now that you know the basics you can continue your research on Symfony - ``Request`` and ``Response`` object in the - :ref:`HttpFoundation component documentation `. +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:: + + // returns '{"username":"jane.doe"}' and sets the proper Content-Type header + $data = array('username' => 'jane.doe'); + return $this->json($data); + +The only required argument is the data to be sent, but ``json()`` defines three +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 --------------------- From 8645cdd9bb65f4876a4503299685a5378623f996 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 21 May 2016 11:24:52 +0200 Subject: [PATCH 05/10] Simplified everything --- book/controller.rst | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/book/controller.rst b/book/controller.rst index c33f8864a84..487d310d08e 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -774,6 +774,9 @@ 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`. @@ -781,28 +784,26 @@ There are also special classes to make certain kinds of responses easier: :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:: - // 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'); + return $this->json($data); + } The only required argument is the data to be sent, but ``json()`` defines three 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 --------------------- From 8e7f397993b001610292db4ea650762759b0848d Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 21 May 2016 12:09:02 +0200 Subject: [PATCH 06/10] Fixes --- book/controller.rst | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/book/controller.rst b/book/controller.rst index 487d310d08e..3f03eefb1f0 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -766,12 +766,6 @@ headers and content that's sent back to the client:: $response = new Response(''); $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 `. - There are also special classes to make certain kinds of responses easier: * For JSON, there is :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`. @@ -795,14 +789,17 @@ a ``JsonResponse()`` and encodes the given contents automatically:: public function indexAction() { // returns '{"username":"jane.doe"}' and sets the proper Content-Type header - $data = array('username' => 'jane.doe'); - return $this->json($data); + return $this->json(array('username' => 'jane.doe')); + + // the shortcut defines three optional arguments + // return $this->json($data, $status = 200, $headers = array(), $context = array()); } -The only required argument is the data to be sent, but ``json()`` defines three -more optional arguments:: +.. seealso:: - $this->json($data, $status = 200, $headers = array(), $context = array()); + Now that you know the basics you can continue your research on Symfony + ``Request`` and ``Response`` object in the + :ref:`HttpFoundation component documentation `. Creating Static Pages --------------------- From ee1e66588f25bfe821e2fad6fff1f2664df77492 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 21 May 2016 12:09:59 +0200 Subject: [PATCH 07/10] Fixed minor typo --- book/controller.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/controller.rst b/book/controller.rst index 3f03eefb1f0..6c56cbe898b 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -778,7 +778,7 @@ There are also special classes to make certain kinds of responses easier: :class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`. See :ref:`streaming-response`. -JSON helper +JSON Helper ~~~~~~~~~~~ Returning JSON contents is increasingly popular for API-based applications. For From 4b0ba225550e6dc809c7445d36ba91dd4d2a9a48 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 21 May 2016 12:12:11 +0200 Subject: [PATCH 08/10] Fixed typo --- book/controller.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/controller.rst b/book/controller.rst index 6c56cbe898b..31db8286faf 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -783,7 +783,7 @@ JSON Helper 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:: +a ``JsonResponse`` and encodes the given contents automatically:: // ... public function indexAction() From 7bc2ff84dd51edffd9abc241cdb2a8ee24d18f78 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 21 May 2016 16:09:03 +0200 Subject: [PATCH 09/10] Explained how contents are serialized --- book/controller.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/book/controller.rst b/book/controller.rst index 31db8286faf..425a44bef99 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -795,6 +795,10 @@ a ``JsonResponse`` and encodes the given contents automatically:: // return $this->json($data, $status = 200, $headers = array(), $context = array()); } +If the :doc:`serializer service `_ is enabled in your +application, contents passed to ``json()`` are encoded with it. Otherwise, +the :phpfunction:`json_encode()` function is used. + .. seealso:: Now that you know the basics you can continue your research on Symfony From 0bed9f7014219109e9b9ba852e9a697ac774d783 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 21 May 2016 16:19:19 +0200 Subject: [PATCH 10/10] Syntax issue --- book/controller.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/controller.rst b/book/controller.rst index 425a44bef99..7edddef82a4 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -795,7 +795,7 @@ a ``JsonResponse`` and encodes the given contents automatically:: // return $this->json($data, $status = 200, $headers = array(), $context = array()); } -If the :doc:`serializer service `_ is enabled in your +If the :doc:`serializer service ` is enabled in your application, contents passed to ``json()`` are encoded with it. Otherwise, the :phpfunction:`json_encode()` function is used.