Symfony FOSUserBundle-在布局模板中包含登录表单 [英] Symfony FOSUserBundle - include login form in layout template

查看:65
本文介绍了Symfony FOSUserBundle-在布局模板中包含登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经成功配置了FOSUserBundle;登录,注册,重置密码等都可以正常工作.

We've successfully configured the FOSUserBundle; login, register, reset password, etc, are all working just fine.

现在,我们希望将登录表单合并到我们的常规网站布局中,特别是将表单放置在布局标题的右上部分中.如果我们只处理用户名和密码字段,那么这样做很容易.但是,我们似乎无法弄清楚如何获取由FOSUserBundle服务生成的CSRF令牌:

Now we want to incorporate the login form into our general site layout, notably placing the form into the top-right section of the layout header. Doing this would be easy enough if we were just dealing with the username and password fields. However we can't seem to figure out how to obtain the CSRF token which is generated by the FOSUserBundle service:

$this->container->get('form.csrf_provider')->generateCsrfToken('authenticate');

我尝试在Twig扩展名中调用上述内容,否则该扩展名可以正常工作,但显然该扩展名无法正确引用容器.

I tried calling the above within a Twig extension which otherwise works fine however apparently the extension can't properly reference the container.

肯定有一些简单的方法可以全局获取FOSUserBundle CSRF令牌吗?

Surely there is some easy way to obtain the FOSUserBundle CSRF token globally?

谢谢! 杰森

推荐答案

您可以在一个控制器中定义这样的函数

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'));
}

,然后通过

<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;

这篇关于Symfony FOSUserBundle-在布局模板中包含登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆