I listen to the kernel.controller action to implement a default (or init) action foo my controllers:
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
if (!is_array($controller)) {
return;
}
$currentController = $controller[0];
if($controller[0] instanceof Initmethod){
$ret = $currentController->init($event->getRequest());
if($ret !== null && $ret instanceof Response){
$ret->send();
exit();
}
}
$event->setController($controller);
}
As you can see, i want to give my init action the ability to return a response which should be send instead of the one of the called action. After that no other action should be executed.
My question is, is the way i’m doing it the right one? Because a don’t want to “fight the framework” and ending the execution by calling exit doesn’t seems do be right.
You can set
initmethod of controller as current callable method e.gThen Symfony will process
initmethod insted of default controller method and return the response if exists. I am assuming that you want to call the default controller method ifinitmethod returnsnull. In that case you can get method paremeters of that method using reflection, map function args and then invoke the method.Edit:
It seems that I have misunderstood your problem. You want to avoid other listener operations like
kernel.view,kernel.responseetc. I think your solution is OK. But if you set session variable in yourinitmethod it will not be saved as it done by ContextListener which is called duringkernel.responseevent.