Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSRF Token from the controller

I have a controller getting a form posted.

public function myPostAction(Request $request)
{
    $form = $this->createForm('my_form', $my_object);
    $form->handleRequest($request);
#...

I can see my CSRF token posted as parameter

my_form[_token] => lH38HTm5P0Cv3TOc4-9xi2COx-cZ670mpJ_36gR8ccI

I simply need to read it

$form->get('_token')

This tells me

Child "_token" does not exist.

How can I get this token ?

like image 375
Pierre de LESPINAY Avatar asked Feb 18 '15 18:02

Pierre de LESPINAY


1 Answers

Here is the workaround I'm going to use meanwhile:

$token = $request->get($form->getName())['_token'];

I also noticed by chance that the intention used to generate the token is the form name

$csrf = $this->get('form.csrf_provider');
$intention = $form->getName();
$token = $csrf->generateCsrfToken($intention);
like image 192
Pierre de LESPINAY Avatar answered Oct 02 '22 21:10

Pierre de LESPINAY