I'm using the event listener onKernelResponse
.
I used :
if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
return;
}
It's having a MASTER_REQUEST
twice in my action, there is one before the <!DOCTYPE html> <html> <head>
etc, and the other one as excepted after the end of the layout.
He is my services.yml
:
history.listener:
class: VENDOR\MyBundle\Service\HistoryListener
arguments: [@doctrine.orm.entity_manager, @logger, @history]
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }
Did I do something wrong?
Finally found the origin of the problem : the debug toolbar !
It actually sends an ajax request, meaning another MASTER_REQUEST
..
My solution is to filter on Controller, with a white/black list of controller's names.
UPDATE:
Here is the code I'm using (so you can easily exclude some other controllers if needed).
public function __construct()
{
$this->classesExcluded = array("Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController");
}
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
if (!is_array($controller) || HttpKernelInterface::MASTER_REQUEST != $event->getRequestType() || in_array(get_class($controller[0]), $this->classesExcluded)) {
return;
}
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With