@@ -256,7 +256,7 @@ The controller has a single argument, ``$name``, which corresponds to the
256
256
``{name} `` placeholder from the matched route (e.g. ``ryan `` if you go to
257
257
``/hello/ryan ``). When executing the controller, Symfony matches each argument
258
258
with 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
260
260
same as the name of the argument variable.
261
261
262
262
Take the following more-interesting example, where the controller has two
@@ -321,42 +321,42 @@ Keep the following guidelines in mind while you develop.
321
321
322
322
#. **The order of the controller arguments does not matter **
323
323
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::
327
327
328
- public function indexAction($lastName, $firstName)
329
- {
330
- // ...
331
- }
328
+ public function indexAction($lastName, $firstName)
329
+ {
330
+ // ...
331
+ }
332
332
333
333
#. **Each required controller argument must match up with a routing parameter **
334
334
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::
337
337
338
- public function indexAction($firstName, $lastName, $foo)
339
- {
340
- // ...
341
- }
338
+ public function indexAction($firstName, $lastName, $foo)
339
+ {
340
+ // ...
341
+ }
342
342
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::
345
345
346
- public function indexAction($firstName, $lastName, $foo = 'bar')
347
- {
348
- // ...
349
- }
346
+ public function indexAction($firstName, $lastName, $foo = 'bar')
347
+ {
348
+ // ...
349
+ }
350
350
351
351
#. **Not all routing parameters need to be arguments on your controller **
352
352
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::
355
355
356
- public function indexAction($firstName)
357
- {
358
- // ...
359
- }
356
+ public function indexAction($firstName)
357
+ {
358
+ // ...
359
+ }
360
360
361
361
.. tip ::
362
362
@@ -431,10 +431,6 @@ If you want to redirect the user to another page, use the ``redirectToRoute()``
431
431
// return $this->redirect($this->generateUrl('homepage'));
432
432
}
433
433
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
-
438
434
By default, the ``redirectToRoute() `` method performs a 302 (temporary) redirect. To
439
435
perform a 301 (permanent) redirect, modify the third argument::
440
436
@@ -500,8 +496,8 @@ The Symfony templating engine is explained in great detail in the
500
496
.. sidebar :: Templating Naming Pattern
501
497
502
498
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 ``
505
501
and ``Resources/views/layout.html.twig `` inside the bundle respectively.
506
502
507
503
.. index ::
@@ -537,6 +533,14 @@ console command:
537
533
538
534
For more information, see the :doc: `/book/service_container ` chapter.
539
535
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
+
540
544
.. index ::
541
545
single: Controller; Managing errors
542
546
single: Controller; 404 pages
@@ -762,9 +766,9 @@ headers and content that's sent back to the client::
762
766
// create a simple Response with a 200 status code (the default)
763
767
$response = new Response('Hello '.$name, Response::HTTP_OK);
764
768
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 ');
768
772
769
773
There are also special classes to make certain kinds of responses easier:
770
774
@@ -778,6 +782,30 @@ There are also special classes to make certain kinds of responses easier:
778
782
:class: `Symfony\\ Component\\ HttpFoundation\\ StreamedResponse `.
779
783
See :ref: `streaming-response `.
780
784
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
+
781
809
.. seealso ::
782
810
783
811
Now that you know the basics you can continue your research on Symfony
0 commit comments