Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 : onKernelResponse called twice as MASTER_REQUEST

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?

like image 827
Bonswouar Avatar asked Jan 15 '14 09:01

Bonswouar


1 Answers

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;
    }
  // ...
}
like image 100
Bonswouar Avatar answered Oct 22 '22 07:10

Bonswouar