In FOSUserBundle, how to create an alternative logout

Using FOSUserBundle and Symfony2,

on my site, depending on the role of the user who is logged in, I want Logout to redirect alternatively to two different pages.

So, I want to do something like this:

{% if is_granted("ROLE_PREMIUM") %} <a href="{{ path('fos_user_security_logout_premium') }}">{{ 'layout.logout'|trans({}, 'FOSUserBundle') }}</a> {% else %} <a href="{{ path('fos_user_security_logout') }}">{{ 'layout.logout'|trans({}, 'FOSUserBundle') }}</a> {% endif %} 

And then, somehow, do something like this:

 <route id="fos_user_security_logout" pattern="/logout"> <default key="_controller">FOSUserBundle:Security:logout</default> </route> <route id="fos_seller_security_logout" pattern="/logoutPremium"> <default key="_controller">FOSUserBundle:Security:logoutPremium</default> </route> 

But, since everything in logout is done in config.yml , config.yml all this configuration into the login and logout, I don’t know how to configure the second trigger and implement it. In fact, the only thing I want to do is redirect the user to two different pages according to the role. Everything else should remain unchanged.

All you need to do here in security.yml ?

  logout: path: /logout target: /main/user 

thanks a lot

+4
source share
1 answer

You can add CustomLogoutHandler to the security.yml configuration.

 firewalls: main: # - the name of your secure area logout: path: /logout target: / success_handler: your_bundle.custom_logout_handler 

Where your_bundle.custom_logout_handler will be responsible for the user your_bundle.custom_logout_handler out and redirecting him according to the same _target_url parameter, for example.

See Symfony/Component/Security/Http/Logout

+4
source

All Articles