Symfony 2 Login on main domain only

2019-06-01 23:06发布

问题:

I am working on a application using multiple subdomains :

  • .domain.com
  • admin.domain.com
  • {username}.domain.com

    and so on.

I am using the FOSUserBundle to handle my users and I am wondering how I could force users to login only on the main .domain.com.

For example, when a user is trying to access the administration, redirect him to http://domain.com/login instead of http://admin.domain.com/login.

I was thinking about a LoginListener somewhere, but I have no idea where I could create it and how to override the login process.

Any ideas ?? Thank you !

回答1:

Add a host directive to your login route.

# app/config/routing.yml
login_route:
    path:     /login
    host:     "domain.com"

Then use this route in your firewall configuration as the login_path:

# app/config/security.yml
security:
    # ...
    firewalls:
        your_firewall_name:
            # ...
            form_login:
                 login_path: login_route
                 # ...

Now everytime the url for login_route is generated it automatically refers to the host domain.com.