Skip to content

Commit 8257cc8

Browse files
Henry Snoekxabbuh
Henry Snoek
authored andcommitted
document old way of checking validity of CSRF token
1 parent 71469ff commit 8257cc8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

book/controller.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,24 @@ Just like when creating a controller for a route, the order of the arguments of
794794
order of the arguments, Symfony will still pass the correct value to each
795795
variable.
796796

797+
Checking the Validity of a CSRF Token
798+
-------------------------------------
799+
800+
Sometimes you want to use CSRF protection in an action where you don't want to use a
801+
Symfony form.
802+
803+
If, for example, you're doing a DELETE action, you can use the
804+
:method:`Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface::isCsrfTokenValid`
805+
method to check the CSRF token::
806+
807+
$csrf = $this->container->get('form.csrf_provider');
808+
$intention = 'authenticate';
809+
$token = $csrf->generateCsrfToken($intention);
810+
811+
if (!$csrf->isCsrfTokenValid($intention, $token)) {
812+
// CSRF token invalid! Do something, like redirect with an error.
813+
}
814+
797815
Final Thoughts
798816
--------------
799817

0 commit comments

Comments
 (0)