ElPiter
ElPiter

Reputation: 4324

In FOSUserBundle, how to create an alternative logout

Using FOSUserBundle and Symfony2,

in my site, depending on the roles of the user that is logged in, I want to make the 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 all in logout is done in config.yml, putting all this configuration about login and logout, I don't know how to configure a second trigger and implement it. Actually, the only thing that I want to do is to redirect the user to two different pages according to the role. All the rest should stay the same.

Anything to put around here in security.yml?

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

Thanks a lot

Upvotes: 4

Views: 10849

Answers (1)

Boris Gu&#233;ry
Boris Gu&#233;ry

Reputation: 47624

You can add a CustomLogoutHandler to your 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 would be in charge to logout your user and redirect him according to a similar _target_url parameter for example.

See Symfony/Component/Security/Http/Logout

Upvotes: 4

Related Questions