Symfony2-FOSUserBundle-多个登录位置 [英] Symfony2 - FOSUserBundle - Multiple Login Locations

查看:48
本文介绍了Symfony2-FOSUserBundle-多个登录位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FOSUserBundle,并且需要能够从2条不同的路由(或更多路由)登录.这些路线将具有不同的模板,并登录到不同的区域.两次登录之间唯一不同的是所需的权限. 路线将类似于

I'm using the FOSUserBundle and I require the ability to login from 2 different routes(or more). These routes will have different templates and also login to different areas. The only thing that differs between the logins is the permission required. The routes will be something along the lines of

site.com/login

site.com/login

site.com/admin/login

site.com/admin/login

,还有可能的site.com/ajax_login

and also possible site.com/ajax_login

通过从FOSUserBundle login.html.twig(被重写)中剔除CSRF令牌之外的所有内容,然后创建呈现其自己的登录框的路由,我已经能够弄清楚如何获得不同的模板进行呈现登录路由(以便仅呈现CSRF令牌). 这对于管理员/登录不起作用,因为该表单回发回登录,并且如果失败,它将显示该页面.

I've been able to work out how to get different templates to render by ripping out everything but the CSRF token from the FOSUserBundle login.html.twig(that is overriden) then creating routes that render their own login boxes and also the login route(so that just the CSRF token gets rendered). This doesnt work for admin/login as the form posts back to login and if it fails it displays that page instead.

有什么简单的方法可以实现这一目标吗?

Is there any easy way to achieve this?

推荐答案

我被困了一段时间,然后自己创建了一个解决方案.我知道必须有一个简单的解决方案...

I was stucked with the same question for a while and then i created a solution on my own. I knew there must be an easy solution...

我已提交拉取请求,使您可以更轻松地创建新的登录模板.在此处检查请求请求: https://github.com/FriendsOfSymfony/FOSUserBundle/pull/1186.

I've submitted pull request which allows you to create new login templates more easily. Check the pull request here: https://github.com/FriendsOfSymfony/FOSUserBundle/pull/1186.

还有另一种非常简单的方法来实现这一目标.扩展SecurityController并更改具有以下内容的renderLogin方法

There is also another quite easy way how to achieve this. Extend SecurityController and change renderLogin method with following content

protected function renderLogin(array $data, $template)
{
    return $this->container->get('templating')->renderResponse('YourBundle:Security:login.html.twig');
}

然后创建到您新创建的控制器的路由:

Then create a route to your newly created controller:

admin.login:
    pattern: /admin/login
    defaults: { _controller: YourBundle:Security:login }

在此之后,您唯一需要做的就是相应地更改安全性配置.将您的form_login login_path更改为/admin/login,就可以了.

After this only you have to do is to alter your security config accordingly. Change your form_login login_path to /admin/login and you are good to go.

这篇关于Symfony2-FOSUserBundle-多个登录位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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