@@ -440,7 +440,7 @@ If you want to redirect the user to another page, use the ``redirectToRoute()``
440
440
}
441
441
442
442
.. versionadded :: 2.6
443
- The ``redirectToRoute() `` method was added in Symfony 2.6. Previously (and still now), you
443
+ The ``redirectToRoute() `` method was introduced in Symfony 2.6. Previously (and still now), you
444
444
could use ``redirect() `` and ``generateUrl() `` together for this (see the example above).
445
445
446
446
Or, if you want to redirect externally, just use ``redirect() `` and pass it the URL::
@@ -619,12 +619,12 @@ session.
619
619
Flash Messages
620
620
~~~~~~~~~~~~~~
621
621
622
- You can also store small messages that will be stored on the user's session.
623
- This is useful when processing a form:
624
- you want to redirect and have a special message shown on the * next * page.
625
- These types of messages are called "flash" messages .
622
+ You can also store special messages, called "flash" messages, on the user's
623
+ session. By design, flash messages are meant to be used exactly once: they vanish
624
+ from the session automatically as soon as you retrieve them. This feature makes
625
+ "flash" messages particularly great for storing user notifications .
626
626
627
- For example, imagine you're processing a form submit ::
627
+ For example, imagine you're processing a form submission ::
628
628
629
629
use Symfony\Component\HttpFoundation\Request;
630
630
@@ -650,20 +650,20 @@ For example, imagine you're processing a form submit::
650
650
return $this->render(...);
651
651
}
652
652
653
- After processing the request, the controller sets a `` notice `` flash message
654
- in the session and then redirects. The name (``notice ``) isn't significant -
655
- it's just something you invent and reference next .
653
+ After processing the request, the controller sets a flash message in the session
654
+ and then redirects. The message key (``notice `` in this example) can be anything:
655
+ you'll use this key to retrieve the message .
656
656
657
- In the template of the next action, the following code could be used to render
658
- the `` notice `` message :
657
+ In the template of the next page (or even better, in your base layout template),
658
+ read any flash messages from the session: :
659
659
660
660
.. configuration-block ::
661
661
662
662
.. code-block :: html+jinja
663
663
664
- {% for flashMessage in app.session.flashbag.get('notice') %}
664
+ {% for flash_message in app.session.flashbag.get('notice') %}
665
665
<div class="flash-notice">
666
- {{ flashMessage }}
666
+ {{ flash_message }}
667
667
</div>
668
668
{% endfor %}
669
669
@@ -677,9 +677,9 @@ the ``notice`` message:
677
677
678
678
.. note ::
679
679
680
- By design, flash messages are meant to be processed exactly once. This means
681
- that they vanish from the session automatically when they are retrieved from
682
- the flash bag by calling the `` get() `` method .
680
+ It's common to use `` notice ``, `` warning `` and `` error `` as the keys of the
681
+ different types of flash messages, but you can use any key that fits your
682
+ needs .
683
683
684
684
.. tip ::
685
685
@@ -811,6 +811,29 @@ Just like when creating a controller for a route, the order of the arguments of
811
811
order of the arguments, Symfony will still pass the correct value to each
812
812
variable.
813
813
814
+ Validating a CSRF Token
815
+ -----------------------
816
+
817
+ Sometimes, you want to use CSRF protection in an action where you don't want to
818
+ use the Symfony Form component. If, for example, you're doing a DELETE action,
819
+ you can use the :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::isCsrfTokenValid `
820
+ method to check the CSRF token::
821
+
822
+ if ($this->isCsrfTokenValid('token_id', $submittedToken)) {
823
+ // ... do something, like deleting an object
824
+ }
825
+
826
+ .. versionadded :: 2.6
827
+ The ``isCsrfTokenValid() `` shortcut method was introduced in Symfony 2.6.
828
+ It is equivalent to executing the following code:
829
+
830
+ .. code-block :: php
831
+
832
+ use Symfony\Component\Security\Csrf\CsrfToken;
833
+
834
+ $this->get('security.csrf.token_manager')
835
+ ->isTokenValid(new CsrfToken('token_id', 'TOKEN'));
836
+
814
837
Final Thoughts
815
838
--------------
816
839
0 commit comments