Trying to swap a controller using an event listener with Symfony2

Viewed 5758

I've been reading through the internals chapter in the Symfony2 docs and it says if I add a listener to the kernel.controller event I can swap the controller that gets run, I've got something that works a bit like this:

public function onKernelController(FilterControllerEvent $event)    
{
    $controller = $event->getController();

    $replacementControllerName = .... //Some logic to work out the name of the new controller
    $replacementController = ?? //Not sure what goes here

    $event->setController($replacementController);
}

The bit I'm unsure if is once I've worked out the name of the replacement controller, how do I get an instance of it that I can pass to setController?

1 Answers
Related