BleuBizarre
BleuBizarre

Reputation: 378

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,

Upvotes: 0

Views: 58

Answers (1)

Rvanlaak
Rvanlaak

Reputation: 3085

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') %}

Upvotes: 2

Related Questions