@@ -256,7 +256,7 @@ The controller has a single argument, ``$name``, which corresponds to the
256256``{name} `` placeholder from the matched route (e.g. ``ryan `` if you go to
257257``/hello/ryan ``). When executing the controller, Symfony matches each argument
258258with a placeholder from the route. So the value for ``{name} `` is passed
259- to ``$name ``. Just make sure they the name of the placeholder is the
259+ to ``$name ``. Just make sure that the name of the placeholder is the
260260same as the name of the argument variable.
261261
262262Take the following more-interesting example, where the controller has two
@@ -321,42 +321,42 @@ Keep the following guidelines in mind while you develop.
321321
322322#. **The order of the controller arguments does not matter **
323323
324- Symfony matches the parameter **names ** from the route to the variable
325- **names ** of the controller. The arguments of the controller could be
326- totally reordered and still work perfectly::
324+ Symfony matches the parameter **names ** from the route to the variable
325+ **names ** of the controller. The arguments of the controller could be
326+ totally reordered and still work perfectly::
327327
328- public function indexAction($lastName, $firstName)
329- {
330- // ...
331- }
328+ public function indexAction($lastName, $firstName)
329+ {
330+ // ...
331+ }
332332
333333#. **Each required controller argument must match up with a routing parameter **
334334
335- The following would throw a ``RuntimeException `` because there is no
336- ``foo `` parameter defined in the route::
335+ The following would throw a ``RuntimeException `` because there is no
336+ ``foo `` parameter defined in the route::
337337
338- public function indexAction($firstName, $lastName, $foo)
339- {
340- // ...
341- }
338+ public function indexAction($firstName, $lastName, $foo)
339+ {
340+ // ...
341+ }
342342
343- Making the argument optional, however, is perfectly ok. The following
344- example would not throw an exception::
343+ Making the argument optional, however, is perfectly ok. The following
344+ example would not throw an exception::
345345
346- public function indexAction($firstName, $lastName, $foo = 'bar')
347- {
348- // ...
349- }
346+ public function indexAction($firstName, $lastName, $foo = 'bar')
347+ {
348+ // ...
349+ }
350350
351351#. **Not all routing parameters need to be arguments on your controller **
352352
353- If, for example, the ``lastName `` weren't important for your controller,
354- you could omit it entirely::
353+ If, for example, the ``lastName `` weren't important for your controller,
354+ you could omit it entirely::
355355
356- public function indexAction($firstName)
357- {
358- // ...
359- }
356+ public function indexAction($firstName)
357+ {
358+ // ...
359+ }
360360
361361.. tip ::
362362
@@ -431,10 +431,6 @@ If you want to redirect the user to another page, use the ``redirectToRoute()``
431431 // return $this->redirect($this->generateUrl('homepage'));
432432 }
433433
434- .. versionadded :: 2.6
435- The ``redirectToRoute() `` method was introduced in Symfony 2.6. Previously (and still now), you
436- could use ``redirect() `` and ``generateUrl() `` together for this (see the example above).
437-
438434By default, the ``redirectToRoute() `` method performs a 302 (temporary) redirect. To
439435perform a 301 (permanent) redirect, modify the third argument::
440436
@@ -500,8 +496,8 @@ The Symfony templating engine is explained in great detail in the
500496.. sidebar :: Templating Naming Pattern
501497
502498 You can also put templates in the ``Resources/views `` directory of a bundle and
503- reference them with a special shortcut syntax like ``@AppBundle /Hello/index.html.twig ``
504- or ``@AppBundle /layout.html.twig ``. These would live in at ``Resources/views/Hello/index.html.twig ``
499+ reference them with a special shortcut syntax like ``@App /Hello/index.html.twig ``
500+ or ``@App /layout.html.twig ``. These would live in at ``Resources/views/Hello/index.html.twig ``
505501 and ``Resources/views/layout.html.twig `` inside the bundle respectively.
506502
507503.. index ::
@@ -537,6 +533,14 @@ console command:
537533
538534 For more information, see the :doc: `/book/service_container ` chapter.
539535
536+ .. tip ::
537+
538+ To get a container configuration parameter in controller you can use the
539+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::getParameter `
540+ method::
541+
542+ $from = $this->getParameter('app.mailer.from');
543+
540544.. index ::
541545 single: Controller; Managing errors
542546 single: Controller; 404 pages
@@ -762,9 +766,9 @@ headers and content that's sent back to the client::
762766 // create a simple Response with a 200 status code (the default)
763767 $response = new Response('Hello '.$name, Response::HTTP_OK);
764768
765- // create a JSON -response with a 200 status code
766- $response = new Response(json_encode(array('name' => $name)) );
767- $response->headers->set('Content-Type', 'application/json ');
769+ // create a CSS -response with a 200 status code
770+ $response = new Response('<style> ... </style>' );
771+ $response->headers->set('Content-Type', 'text/css ');
768772
769773There are also special classes to make certain kinds of responses easier:
770774
@@ -778,6 +782,30 @@ There are also special classes to make certain kinds of responses easier:
778782 :class: `Symfony\\ Component\\ HttpFoundation\\ StreamedResponse `.
779783 See :ref: `streaming-response `.
780784
785+ JSON Helper
786+ ~~~~~~~~~~~
787+
788+ .. versionadded :: 3.1
789+ The ``json() `` helper was introduced in Symfony 3.1.
790+
791+ Returning JSON contents is increasingly popular for API-based applications. For
792+ that reason, the base controller class defines a ``json() `` method which creates
793+ a ``JsonResponse `` and encodes the given contents automatically::
794+
795+ // ...
796+ public function indexAction()
797+ {
798+ // returns '{"username":"jane.doe"}' and sets the proper Content-Type header
799+ return $this->json(array('username' => 'jane.doe'));
800+
801+ // the shortcut defines three optional arguments
802+ // return $this->json($data, $status = 200, $headers = array(), $context = array());
803+ }
804+
805+ If the :doc: `serializer service </cookbook/serializer >` is enabled in your
806+ application, contents passed to ``json() `` are encoded with it. Otherwise,
807+ the :phpfunction: `json_encode ` function is used.
808+
781809.. seealso ::
782810
783811 Now that you know the basics you can continue your research on Symfony
0 commit comments