@@ -54,12 +54,11 @@ Every request handled by a Symfony project goes through the same simple lifecycl
54
54
The framework takes care of all the repetitive stuff: you just need to write
55
55
your custom code in the controller function:
56
56
57
- #. Each request executes a single front controller file (e.g. on production
58
- ``app.php `` or on development ``app_dev.php ``) that bootstraps the
59
- application;
57
+ #. Each request executes a single front controller file (e.g. ``app.php `` on production
58
+ or ``app_dev.php `` on development) that bootstraps the application;
60
59
61
- #. Front controller's only job is to initialize Symfony's engine (called the
62
- `Kernel `) and pass it a ``Request `` object to handle;
60
+ #. The front controller's only job is to initialize Symfony's engine (called the
61
+ `` Kernel ` `) and pass it a ``Request `` object to handle;
63
62
64
63
#. The Symfony core asks the router to inspect the request;
65
64
@@ -80,18 +79,20 @@ that maps a URL to that controller (#4).
80
79
.. image :: /images/http-xkcd-request.png
81
80
:align: center
82
81
83
- Though similarly named, a "front controller" is different from the PHP
84
- functions called "controllers" talked about in this chapter. A front
85
- controller is a short PHP file that lives in your ``web/ `` directory
86
- through which all requests are directed. A typical application will
87
- have a production front controller (e.g. ``app.php ``) and a development
88
- front controller (e.g. ``app_dev.php ``). You'll likely never need to
89
- edit, view or worry about the front controllers in your application.
90
- The "controller class" is a convenient way to group several "controllers",
91
- also called actions, together in one class (e.g. ``updateAction() ``,
92
- ``deleteAction() ``, etc). So, a controller is a method inside a controller
93
- class. They hold your code which creates and returns the appropriate
94
- ``Response `` object.
82
+ .. note ::
83
+
84
+ Though similarly named, a "front controller" is different from the PHP
85
+ functions called "controllers" talked about in this chapter. A front
86
+ controller is a short PHP file that lives in your ``web/ `` directory
87
+ through which all requests are directed. A typical application will
88
+ have a production front controller (e.g. ``app.php ``) and a development
89
+ front controller (e.g. ``app_dev.php ``). You'll likely never need to
90
+ edit, view or worry about the front controllers in your application.
91
+ The "controller class" is a convenient way to group several "controllers",
92
+ also called actions, together in one class (e.g. ``updateAction() ``,
93
+ ``deleteAction() ``, etc). So, a controller is a method inside a controller
94
+ class. They hold your code which creates and returns the appropriate
95
+ ``Response `` object.
95
96
96
97
.. index ::
97
98
single: Controller; Simple example
@@ -150,7 +151,7 @@ Mapping a URL to a Controller
150
151
151
152
The new controller returns a simple HTML page. To actually view this page
152
153
in your browser, you need to create a route, which maps a specific URL path
153
- to the controller::
154
+ to the controller:
154
155
155
156
.. configuration-block ::
156
157
@@ -218,17 +219,14 @@ simply creating a controller method and an associated route.
218
219
219
220
Simple, right?
220
221
221
- .. sidebar :: Logical controller name
222
+ .. sidebar :: The AppBundle:Hello:index controller syntax
222
223
223
224
If you use the YAML or XML formats, you'll refer to the controller
224
- using a special shortcut syntax called *logical controller name *
225
+ using a special shortcut syntax called the *logical controller name *
225
226
which, for example, looks like ``AppBundle:Hello:index ``. For more
226
227
details on the controller format, read
227
228
:ref: `controller-string-syntax ` subtitle of the Routing chapter.
228
229
229
- You can learn much more about the routing system in the
230
- :doc: `Routing chapter </book/routing >`.
231
-
232
230
.. index ::
233
231
single: Controller; Controller arguments
234
232
@@ -255,13 +253,13 @@ method::
255
253
}
256
254
257
255
The controller has a single argument, ``$name ``, which corresponds to the
258
- ``{name} `` placehold from the matched route (``ryan `` if you go to
259
- ``/hello/ryan ``). When executing controller, Symfony matches each argument
260
- with a placehold from the route. So the value for ``{name} `` is passed
256
+ ``{name} `` placeholder from the matched route (e.g. ``ryan `` if you go to
257
+ ``/hello/ryan ``). When executing the controller, Symfony matches each argument
258
+ with a placeholder from the route. So the value for ``{name} `` is passed
261
259
to ``$name ``. Just make sure they the name of the placeholder is the
262
260
same as the name of the argument variable.
263
261
264
- Take the following more-interesting example, where controller has two
262
+ Take the following more-interesting example, where the controller has two
265
263
arguments::
266
264
267
265
.. configuration-block ::
@@ -362,11 +360,6 @@ Keep the following guidelines in mind while you develop.
362
360
363
361
.. tip ::
364
362
365
- Every route also has a special ``_route `` parameter (you will learn more
366
- about this in the :ref: `Routing chapter <book-special-routing-parameters >`),
367
- which is equal to the name of the route that was matched (e.g. ``hello ``).
368
- Though not usually useful, this is also available as a controller argument.
369
-
370
363
You can also pass other variables from your route to your controller
371
364
arguments. See :doc: `/cookbook/routing/extra_information `.
372
365
@@ -379,7 +372,7 @@ The Base Controller Class
379
372
380
373
For convenience, Symfony comes with an optional base
381
374
:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` class.
382
- If you extend it, this wont change anything about how your controller
375
+ If you extend it, this won't change anything about how your controller
383
376
works, but you'll get access to a number of **helper methods ** and the
384
377
**service container ** (see :ref: `controller-accessing-services `): an
385
378
array-like object that gives you access to every useful object in the
@@ -388,7 +381,7 @@ with a service object that can render Twig templates, another that can
388
381
log messages and many more.
389
382
390
383
Add the ``use `` statement atop the ``Controller `` class and then modify
391
- the ``HelloController `` to extend it::
384
+ ``HelloController `` to extend it::
392
385
393
386
// src/AppBundle/Controller/HelloController.php
394
387
namespace AppBundle\Controller;
@@ -428,10 +421,10 @@ method is just a helper method that generates the URL for a given route.
428
421
Redirecting
429
422
~~~~~~~~~~~
430
423
431
- To redirect the user's browser to another page ** internally ** , use the ``generateUrl() ``
424
+ To redirect the user's browser to another page of your app , use the ``generateUrl() ``
432
425
method in combination with another helper method called
433
426
:method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::redirect `
434
- which needs URL as an argument::
427
+ which takes a URL as an argument::
435
428
436
429
public function indexAction()
437
430
{
@@ -446,7 +439,7 @@ perform a 301 (permanent) redirect, modify the second argument::
446
439
return $this->redirect($this->generateUrl('homepage'), 301);
447
440
}
448
441
449
- To redirect ** externally ** , use ``redirect() `` and pass it the external URL::
442
+ To redirect to an * external * site , use ``redirect() `` and pass it the external URL::
450
443
451
444
public function indexAction()
452
445
{
@@ -472,15 +465,14 @@ For more information, see the :doc:`Routing chapter </book/routing>`.
472
465
Rendering Templates
473
466
~~~~~~~~~~~~~~~~~~~
474
467
475
- To serve HTML, template needs to be rendered and the content put into
476
- the ``Response `` object. The shortcut method
477
- :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::render `
478
- of ``Controller `` class does just that::
468
+ If you're serving HTML, you'll want to render a template. The ``render() ``
469
+ method renders a template **and ** puts that content into a ``Response ``
470
+ object for you::
479
471
480
472
// renders app/Resources/views/hello/index.html.twig
481
473
return $this->render('hello/index.html.twig', array('name' => $name));
482
474
483
- Templates can be also put in deeper sub-directories. Just try to avoid
475
+ Templates can also live in deeper sub-directories. Just try to avoid
484
476
creating unnecessarily deep structures::
485
477
486
478
// renders app/Resources/views/hello/greetings/index.html.twig
@@ -491,29 +483,18 @@ creating unnecessarily deep structures::
491
483
Templates are a generic way to render content in *any * format. And while in
492
484
most cases you'll use templates to render HTML content, a template can just
493
485
as easily generate JavaScript, CSS, XML or any other format you can dream of.
494
- To learn how to render different templating formats read :ref: `template-formats `
486
+ To learn how to render different templating formats read the :ref: `template-formats `
495
487
section of the Creating and Using Templates chapter.
496
488
497
489
The Symfony templating engine is explained in great detail in the
498
490
:doc: `Creating and Using Templates chapter </book/templating >`.
499
491
500
492
.. sidebar :: Templating Naming Pattern
501
493
502
- Templates for a bundle can be put in the ``src/path/to/bundle/Resources/views ``
503
- directory of a bundle and reference with a special shortcut syntax called
504
- *logical name * which, for example, looks like ``AppBundle:Hello:index.html.twig `` or
505
- ``AppBundle::layout.html.twig ``. The pattern has three parts, each separated
506
- by a colon. Syntax used to specify a template for a specific page::
507
-
508
- **bundle**:**directory**:**filename**
509
-
510
- Syntax used to refer to a base template that's specific to the bundle::
511
-
512
- ``**bundle**::**filename**``
513
-
514
- For more details on the template format, read
515
- :ref: `template-referencing-in-bundle ` subtitle of the Creating and
516
- Using Templates chapter.
494
+ You can also put templates in the ``Resources/views `` directory of a bundle and
495
+ reference them with a special shortcut syntax like ``@AppBundle/Hello/index.html.twig ``
496
+ or ``@AppBundle/layout.html.twig ``. These would live in at ``Resources/views/Hello/index.html.twig ``
497
+ and ``Resources/views/layout.html.twig `` inside the bundle respectively.
517
498
518
499
.. index ::
519
500
single: Controller; Accessing services
@@ -540,7 +521,7 @@ need::
540
521
$mailer = $this->get('mailer');
541
522
542
523
What other services exist? To list all services, use the ``container:debug ``
543
- console command::
524
+ console command:
544
525
545
526
.. code-block :: bash
546
527
@@ -583,11 +564,11 @@ Symfony will automatically return a 500 HTTP response code.
583
564
throw new \Exception('Something went wrong!');
584
565
585
566
In every case, an error page is shown to the end user and a full debug
586
- error page is shown to the developer (i.e. when you're using ``app_dev.php ``
567
+ error page is shown to the developer (i.e. when you're using the ``app_dev.php ``
587
568
front controller - see :ref: `page-creation-environments `).
588
569
589
570
You'll want to customize the error page your user sees. To do that, see
590
- the cookbook article ":doc: `/cookbook/controller/error_pages `" cookbook recipe.
571
+ the ":doc: `/cookbook/controller/error_pages `" cookbook recipe.
591
572
592
573
.. index ::
593
574
single: Controller; The session
@@ -620,11 +601,11 @@ about the user (be it a real person using a browser, a bot, or a web service)
620
601
between requests. By default, Symfony stores the attributes in a cookie
621
602
by using the native PHP sessions.
622
603
623
- To retrieving the session we have to call the
604
+ To retrieve the session, call
624
605
:method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::getSession `
625
- method on the ``Request `` object inside a controller. Method returns a
626
- :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionInterface `
627
- with all the methods to handle a session::
606
+ method on the ``Request `` object. This method returns a
607
+ :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionInterface ` with easy
608
+ methods for storing and fetching things from the session::
628
609
629
610
use Symfony\Component\HttpFoundation\Request;
630
611
@@ -722,10 +703,9 @@ read any flash messages from the session:
722
703
The Request and Response Object
723
704
-------------------------------
724
705
725
- As already mentioned a :ref: `little earlier <book-controller-request-argument >`,
726
- besides the values of the routing parameters, the controller has also access
727
- to the ``Request `` object. The framework injects the ``Request `` object
728
- in the controller if a variable is type-hinted with ``Request `` class::
706
+ As mentioned :ref: `earlier <book-controller-request-argument >`, the framework will
707
+ pass the ``Request `` object to any controller argument taht is type-hinted with
708
+ the ``Request `` class::
729
709
730
710
use Symfony\Component\HttpFoundation\Request;
731
711
@@ -753,14 +733,14 @@ in the controller if a variable is type-hinted with ``Request`` class::
753
733
$request->headers->get('content_type');
754
734
}
755
735
756
- ``Request `` class has several public properties via which information about the client
757
- request can be accessed .
736
+ The ``Request `` class has several public properties and methods that return any
737
+ information you need about the request .
758
738
759
- Like the ``Request ``, the ``Response `` object has also a public ``headers `` property
760
- which is a :class: `Symfony\\ Component\\ HttpFoundation\\ ResponseHeaderBag ` instance.
761
- `` ResponseHeaderBag `` instances have methods for getting and setting the response
762
- headers. The header names are normalized so that using ``Content-Type `` is equivalent
763
- to `` content-type `` or even ``content_type ``.
739
+ Like the ``Request ``, the ``Response `` object has also a public ``headers `` property.
740
+ This is a :class: `Symfony\\ Component\\ HttpFoundation\\ ResponseHeaderBag ` that has
741
+ some nice methods for getting and setting response headers. The header names are
742
+ normalized so that using ``Content-Type `` is equivalent to `` content-type `` or even
743
+ ``content_type ``.
764
744
765
745
The only requirement for a controller is to return a ``Response `` object.
766
746
The :class: `Symfony\\ Component\\ HttpFoundation\\ Response ` class is an
@@ -807,15 +787,11 @@ and template are needed). See cookbook article
807
787
Forwarding to Another Controller
808
788
--------------------------------
809
789
810
- We already saw how to redirect the :ref: `user's browser <book-redirecting-users-browser >`
811
- to another page internally or to some external URL.
812
-
813
790
Though not very common, you can also forward to another controller
814
- internally with the basic ``Controller `` class method
815
- :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::forward `.
816
- Instead of redirecting the user's browser, method makes an internal
817
- sub-request, and calls the defined controller. The ``forward() `` method returns
818
- the ``Response `` object that's returned from *that * controller::
791
+ internally with the :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::forward `
792
+ method. Instead of redirecting the user's browser, this makes an "internal" sub-request
793
+ and calls the defined controller. The ``forward() `` method returns the ``Response ``
794
+ object that's returned from *that * controller::
819
795
820
796
public function indexAction($name)
821
797
{
@@ -829,38 +805,25 @@ the ``Response`` object that's returned from *that* controller::
829
805
return $response;
830
806
}
831
807
832
- The array passed to the method becomes the arguments on the resulting controller.
833
- The target controller method would look something like this::
808
+ The array passed to the method becomes the arguments for the resulting controller.
809
+ The target controller method might look something like this::
834
810
835
811
public function fancyAction($name, $color)
836
812
{
837
813
// ... create and return a Response object
838
814
}
839
815
840
- .. sidebar :: Logical controller name
841
-
842
- Notice that the ``forward() `` method uses a special string representation
843
- called *logical controller name * which, for example, looks like
844
- ``AppBundle:Hello:index ``. For more details on the controller format, read
845
- :ref: `controller-string-syntax ` subtitle of the Routing chapter.
846
-
847
- You can learn much more about the routing system in the
848
- :doc: `Routing chapter </book/routing >`.
849
-
850
- Just like when creating a controller for a route, the order of the
851
- arguments of ``fancyAction() `` doesn't matter. Symfony matches the route
852
- placeholder names (e.g. ``{name} ``) with the method argument names (e.g. ``$name ``).
853
- If you change the order of the arguments, Symfony will still pass the correct
854
- value to each variable.
816
+ Just like when creating a controller for a route, the order of the arguments of
817
+ ``fancyAction() `` doesn't matter: the matching is done by name.
855
818
856
819
Checking the Validity of a CSRF Token inside Controller
857
820
-------------------------------------------------------
858
821
859
- Sometimes you want to use :ref: `CSRF protection <forms-csrf >` in a controller where
860
- you don't want to use a Symfony form.
822
+ You may sometimes want to use :ref: `CSRF protection <forms-csrf >` in a controller where
823
+ you don't have a Symfony form.
861
824
862
825
If, for example, you're doing a DELETE action, you can use the
863
- :method: `Symfony\\ Component\\ Form\\ Extension\\ Csrf\\ CsrfProvider\\ SessionCsrfProvider ::isCsrfTokenValid `
826
+ :method: `Symfony\\ Component\\ Form\\ Extension\\ Csrf\\ CsrfProvider\\ CsrfProviderInterface ::isCsrfTokenValid `
864
827
method to check the CSRF token::
865
828
866
829
$csrf = $this->container->get('form.csrf_provider');
@@ -894,4 +857,5 @@ Learn more from the Cookbook
894
857
895
858
* :doc: `/cookbook/controller/error_pages `
896
859
* :doc: `/cookbook/controller/service `
860
+
897
861
.. _`Controller class` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
0 commit comments