简体   繁体   中英

How to have one URL for two controllers depending of roles in Symfony?

I'm trying to create my WebSite on Symfony 2.7. I want to have two "home" with the basic url : "/". One for those who have the role ROLE_USER ( the connected ) and one for those who have IS_AUTHENTICATED_ANONYMOUSLY.

I'm thinking about one URL that calls two controllers instead of calling the same controller that will show the first or second view depending of rights ( I think that it's not a good practice ).

Does someone know how to do it?

I don't even know if I'm going in the good practice so if you to lead me to another way, feel free, thanks.

Best Regards,

This is the wrong way around, because Symfony will first try to match your route against a configured firewall , and then after doing the authentication will check the proper ROLE_USER authorization.

Because you want to allow IS_AUTHENTICATED_ANONYMOUSLY , you should check for ROLE_USER in your controller or template. This is not something you can check based on the same URL.

In your controller:

if ($this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
    // ...
}

In your Twig template:

{% if is_granted('ROLE_USER') %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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