dsh102123 2018-04-05 16:31
浏览 98
已采纳

多个控制器中的Symfony 3登录表单

I followed this tutorial on how to build a login form in Symfony 3 and everything works fine.

However I'd like to use the same login form on multiple pages ("/" and "/login"). Unfortunately I can always get only one of these pages working based on how I configure the "login_path" and "check_path" in security.yaml.

So it's either

form_login:
                  login_path: login
                  check_path: login

or

form_login:
                  login_path: /
                  check_path: /

I've tried to add another firewall specifically for the login page to use both at once, but without success, my form on /login would then seem to submit and would redirect to default_target_path, but the user wouldn't be logged in. I've also tried to set the 'action' variable in the function createForm in controller to make the login form submit to /login even on the index page, but that didn't work either.

Any ideas on how to solve this issue ?

  • 写回答

2条回答 默认 最新

  • dsm0688 2018-04-05 18:13
    关注

    Have you tried creating a second entry with a different name? I'm not sure it will work, but technically different providers with different names are allowed. And there's no requirement they actually have different providers or different implementations.

    security:
        # ...
    
        firewalls:
            main:
                # ...
                form_login:
                    login_path: login
                    check_path: login
            alternative
                # ...
                form_login:
                    login_path: /
                    check_path: /
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douba6365 2018-04-05 19:02
    关注

    You could redirect the user to the login page if they are not logged in.

    //Assuming this is the action for the "/" route
    
    public function indexAction($name)
    {
      //If not logged in
      if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
        return $this->redirectToRoute('/login');  
      }
    
      ...//If logged in
    }
    

    You could possibly just render the login page rather than redirecting.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于python进行多背包问题的多值编码
  • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
  • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
  • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
  • ¥15 C语言:数据子序列基础版
  • ¥20 powerbulider 导入excel文件,显示不完整
  • ¥15 用keil调试程序保证结果进行led相关闪烁
  • ¥15 paddle训练自己的数据loss降不下去
  • ¥20 用matlab的pdetool解决以下三个问题
  • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?