多个控制器中的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 ?

我跟着这个教程,介绍如何在Symfony 3中构建登录表单,一切正常。 p>

但是我喜欢 在多个页面上使用相同的登录表单(“/”和“/ login”)。 不幸的是,根据我在security.yaml中配置“login_path”和“check_path”的方式,我总是只能使用其中一个页面。 p>

所以它是 p>

  form_login:
 login_path:login 
 check_path:login 
  code>  pre>  
 
 

或 p>

  form_login:
 login_path:/ 
 check_path:/ 
  code>  pre> 
 
 

我尝试专门为登录页面添加另一个防火墙,以便同时使用它们,但是没有成功,我/ / login上的表单似乎会提交并重定向到default_target_path,但用户不会登录。 我还尝试在控制器中的createForm函数中设置'action'变量,使登录表单甚至在索引页面上提交到/ login,但这也不起作用。 p> 关于如何解决这个问题的任何想法? p> div>

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: /

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.