Programming Rambling

mrzard's ramblings in the wild

Generate CSRF Token Programatically in Symfony 2

| Comments

If you find yourself in the need of generating a CSRF token for a ‘built’ Request or something in that fashion, you can do it rather easily:

Generate CSRF token programatically
1
2
3

$csrfProvider = $this->container->get('form.csrf_provider');
$csrfToken = $csrfProvider->generateCsrfToken('unknown');

‘unknown’ is the default ‘intention’ of CSRF tokens in Symfony2, change ‘unknown’ for the correct intention if you are using that option.

Comments