n00n
n00n

Reputation: 664

Redirect form LogoutListener

I am learning Symfony and in my example project I have a logout listener to observe logouts.

How can I implement redirection or in this case forwarding to another route?

class LogoutListener implements LogoutHandlerInterface {

    protected $userManager;

    public function logout(Request $request, Response $response, TokenInterface $token) {
        $request->getSession()->invalidate();
        $this->  ....?
    }
}

Upvotes: 1

Views: 868

Answers (2)

n00n
n00n

Reputation: 664

@kalehman

Sorry for my late reply, but I was sick and busy for a while.

I tried your example and I like it, seems to be a better way. But I got an exception wich I could not solve..

In my security.yaml I configured this:

firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager
                default_target_path: /welcome
            logout:
                handlers: [App\Listeners\LogoutListener]
            anonymous:    true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

My Exception:

Argument 1 passed to Symfony\Component\Security\Http\Firewall\LogoutListener::addHandler() must implement interface Symfony\Component\Security\Http\Logout\LogoutHandlerInterface, instance of App\Listeners\LogoutListener given, called in /var/www/symfony/mosys-tool-collection/symfony/var/cache/dev/ContainerXfIwZpI/getSecurity_Firewall_Map_Context_MainService.php on line 30

handlers: [App\Listeners\LogoutListener] is wrong, or? What else do I have to configure?

Upvotes: 1

kalehmann
kalehmann

Reputation: 5011

The LogoutHandlerInterface is not designed for modifying the behavior after an logout.

Take a look at the LogoutSuccessHandlerInterface instead, especially the onLogoutSuccess method.

Use this method to customize the logout behavior and return a RedirectResponse.

For example:

class LogoutListener implements LogoutSuccessHandlerInterface 
{
    public function onLogoutSuccess(Request $request): Response
    {
        $request->getSession()->invalidate();

        return new RedirectResponse('http://mycoolsite.com');
    }
}

Or even better use the generate method of the router to generate an url for your route.

class LogoutListener implements LogoutSuccessHandlerInterface 
{
    protected $router;

    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    public function onLogoutSuccess(Request $request): Response
    {
        $request->getSession()->invalidate();

        return new RedirectResponse(
            $this->router->generate(
                'myroute',
                [],
                UrlGeneratorInterface::ABSOLUTE_PATH
            )
        );
    }
}

Upvotes: 4

Related Questions