Symfony 重定向到通过控制器登录 [英] Symfony redirect to login via controller

查看:34
本文介绍了Symfony 重定向到通过控制器登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行 Symfony 2.5 项目.我的防火墙设置为要求对每个具有/secured/前缀的路由进行身份验证.我有不同类型的用户 ADMIN_ROLE 、 USER_ROLE 和匿名用户.我的需要是将它们中的每一个重定向到不同的页面(如果已登录)和登录屏幕(如果匿名,如果尝试输入路径/web/app.php/

I'm in the middle of a Symfony 2.5 project. My firewall is set to ask the autentication to every route that have /secured/ prefix. I have different types of users ADMIN_ROLE , USER_ROLE and anonymous. My need is to redirect each of them to a different page, if logged and to the login screen if anonymous if the try to enter on the path /web/app.php/

我创建了以下路线:

my_home_redirect:
    pattern:  /
    defaults: { _controller: MyHomeBundle:Default:redirect }

我创建了以下控制器:

public function redirectAction(){
        if ($this->get('security.context')->isGranted('ROLE_ADMIN')){
             $render = $this->forward('MyHomeBundle:Installation:selectInstallation', array('userId'=>$user=$this->get('security.context')->getToken()->getUser()->getId()) );
        }
        elseif($this->get('security.context')->isGranted('ROLE_USER')){
            $render = $this->forward('MyHomeBundle:Installation:selectInstallation', array('userId'=>$user=$this->get('security.context')->getToken()->getUser()->getId()) );
        }
        else {
           // INSERT FORWARD TO THE LOGIN SCREEN
        }
        return $render;
    }

在函数的else"中,如果用户不是管理员也不是用户,我应该将重定向重定向到登录屏幕,但登录由 security.yml 中定义的 symfony 防火墙管理,我没有办法做到这一点.如何在没有显式计数器 loginAction 的情况下重定向到 url .../login?

In the "else" of the function i shoud punt the redirect to the login screen if the user is not an admin and not an user, but the login is managed by the firwall of symfony defined in the security.yml and i have no route to that. How che redirect to the url .../login without having an explicit countroller loginAction?

推荐答案

security.yml中你可以指定一个login_path,例如:

In the security.yml you can specify a login_path, in example:

my_firewall:
    pattern: ^/(secured_area)/
    provider: my_provider
    anonymous: ~
    form_login:
        login_path:  my_login_path
        default_target_path: /dashboard

并且在 routing.yml 中,您可以将路线映射为:

And in the routing.yml you can map the route as:

my_login_path:
    pattern:   /my/relative/url/for/login
    defaults:  { _controller: MySecurityBundle:Security:login }

然后您可以使用普通路线,在您的特定情况下:

then you can use an a normal route, in you specific case:

return $this->forward($this->generateUrl('my_login_path'));

希望对您有帮助

这篇关于Symfony 重定向到通过控制器登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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