【问题标题】:Symfony2 with FOSUserBundle - redirection not working in controller带有 FOSUserBundle 的 Symfony2 - 重定向在控制器中不起作用
【发布时间】:2013-04-27 01:53:12
【问题描述】:

我的默认控制器将路由“/”设置为运行索引:

public function indexAction() {
    if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
        //render the logged in view(s)
        $response = $this->render('Pan100MoodLogBundle:Default:index.html.twig');
    } else {
        //redirect to the login controller
        $response = $this->redirect($this->generateUrl('login'));
    }
    return $response;
}

但是 Symfony 找不到路由:

路由“登录”不存在。 500 内部服务器错误 - RouteNotFoundException

有什么问题吗?我正在使用 FOSUserBundle。

【问题讨论】:

    标签: php symfony fosuserbundle


    【解决方案1】:

    如果你输入

    php app/console router:debug
    

    你会看到你所有的申请路线。

    使用php app/console router:debug | grep login,它显示:

    fos_user_security_login               ANY      /login
    fos_user_security_check               ANY      /login_check
    

    那就试试吧

    $response = $this->redirect($this->generateUrl('fos_user_security_login'));
    

    【讨论】:

      【解决方案2】:

      您可以重定向到fos_user_security_login

      $response = $this->redirect($this->generateUrl('fos_user_security_login'));
      

      【讨论】:

        【解决方案3】:

        看起来,您没有名为“登录”的路线。 你必须在你的 route.yml|xml 文件中创建它,但你不必指定控制器,因为 FOS 会处理它,就像在 yml 中这样:

        login:
            pattern: /login
        

        编辑: 对不起,我的错误,当然不是布局,登录。 但是你在岸上,路线是加载的吗?导致 generateUrl 需要一个路由名称。

        【讨论】:

        • 我把它放在我的配置/路由中,但它不起作用。它按照我在回答中描述的方式工作,但我不知道这是否是正确的做法......
        猜你喜欢
        • 1970-01-01
        • 2016-03-30
        • 2020-06-26
        • 2021-01-17
        • 2014-04-26
        • 1970-01-01
        • 1970-01-01
        • 2017-10-07
        • 1970-01-01
        相关资源
        最近更新 更多