Symfony FOSUserBundle - include login form in layout template

旧巷老猫 提交于 2019-11-28 19:44:02
Daniel

You can define a function like this in one of your controllers

public function getTokenAction()
{
    return new Response($this->container->get('form.csrf_provider')
                            ->generateCsrfToken('authenticate'));
}

and then just embed it into your form via

<input type="hidden" name="_csrf_token" value="{% render('YourBundle:YourController:getToken') %}" />

You also need to include the following at the top of your controller:

use Symfony\Component\HttpFoundation\Response;
David Morales

Symfony 2.3:

One possible solution would be to define the csrf provider as a Twig global variable like this:

twig:
    globals:
        fos_csrf_provider: "@form.csrf_provider"

And then in your layout call it like this:

<input type="hidden" name="_csrf_token" value="{{ fos_csrf_provider.generateCsrfToken('authenticate') }}" />

So you don't need to call any controller.

Symfony 2.4 and later:

twig:
    globals:
        fos_csrf_provider: "@security.csrf.token_manager"

and:

<input type="hidden" name="_csrf_token" value="{{ fos_csrf_provider.refreshToken('authenticate') }}" />
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!