Symfony2 Dynamic Logout Target?

php, symfony

Solution

I needed a Logout Success Handler, and this is how I implemented it:

security.yml:

logout:
    success_handler: acme.security.logout_success_handler

config.yml:

services:
    acme.security.logout_success_handler:
        class: Acme\DefaultBundle\Handler\LogoutSuccessHandler

Symfony/src/Acme/DefaultBundle/Handler/LogoutSuccessHandler.php:

<?php

namespace Acme\DefaultBundle\Handler;

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerAware;

class LogoutSuccessHandler extends ContainerAware implements LogoutSuccessHandlerInterface
{
    public function onLogoutSuccess(Request $request)
    {
        $target_url = $request->query->get('target_url')
                      ? $request->query->get('target_url')
                      : "/";
        return new RedirectResponse($target_url);
    }
}

Problem

I have a working Symfony2 application that properly logs users in and out, and when logging out it properly redirects the user to the home page. I'd like to keep them on their current page when the log out, only without their logged-in privileges. My question is: Can I dynamically set the page the user is directed to when they log out?

Original source