简体   繁体   English

Symfony2:如何将login_check路由名称更改为其他名称并重定向

[英]Symfony2: How to change the login_check route name to others and redirect

Question 1 问题1

How to change the login_check route to other naming route? 如何将login_check路由更改为其他命名路由?

For example, the login form will post to www.example.com/auth instead of www.example.com/login_check. 例如,登录表单将发布到www.example.com/auth,而不是www.example.com/login_check。 Then the auth will perform the checking like login_check. 然后,身份验证将执行类似login_check的检查。

Question 2 问题2

How to redirect user if they try to access /login when they already authenticated? 如果用户已经通过身份验证时尝试访问/ login,如何重定向用户?

For example, when user try to access /login, if he/she already authenticated then will be redirected to /account instead of displaying the login form. 例如,当用户尝试访问/ login时,如果他/她已经通过身份验证,则将被重定向到/ account而不是显示登录表单。

Thank you very much. 非常感谢你。

If you are loading the routes of a bundle inside the src/Pk/AppBundle/Resources/config/routing.yml you can create an entry for the routes: 如果要在src/Pk/AppBundle/Resources/config/routing.yml中加载捆绑的路由,则可以为这些路由创建一个条目:

# src/Pk/AppBundle/Resources/config/routing.yml
homepage:
    pattern:  /
    defaults: { _controller: AppBundle:Home:index }
login:
    pattern:  /login
    defaults: { _controller: AppBundle:Auth:login}
login_check:
    pattern:  /auth
logout:
    pattern:  /logout
account:
    pattern:  /account
    defaults: { _controller: AppBundle:Account:index}

and in your security.yml : 并在您的security.yml

# app/config/security.yml
firewalls:
      main:
          pattern:    ^/
          form_login:
              check_path: login_check       # the name of your check route
              login_path: login             # the name of your login route
              default_target_path: account  # the name of your account route
              always_use_default_target_path: true
          logout:
              path:   logout
              target: /
          anonymous: ~
          remember_me:
              key:      "%secret%"
              lifetime: 31536000 # 365 days in seconds
              path:     /
              domain:   ~ # Defaults to the current domain from $_SERVER

This should work using Symfony 2.6 (Documentation) 这应该可以在Symfony 2.6(文档)中使用

But it will allways redirect after login, if what you want is only when the client GET /login then you can use the controller: 但是它将始终在登录后重定向,如果您只想在客户端GET / login时使用,则可以使用控制器:

# AppBundle:Auth
public function loginAction ()
{
    if($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')){
        return $this->redirect($this->generateUrl('account'));
    }
    // logic of the loginpage
}

If you want more customization you can add a Login Event Listener for it. 如果要进行更多自定义,则可以为其添加“登录事件监听器”。 ( For example ) 例如

If I didn't miss anything it should work. 如果我什么都没错过,那应该可以。

If I get you wrong, please let me know, and I hope this will help you. 如果我弄错了,请告诉我,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM