<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelInterface;
class AjaxSubscriber implements EventSubscriberInterface
{
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
public static function getSubscribedEvents()
{
return [
'kernel.response' => 'onKernelResponse',
];
}
/**
* Add a HTTP header to refresh Symfony Toolbar when Ajax request in dev mode
*/
public function onKernelResponse(ResponseEvent $event)
{
if($this->kernel->getEnvironment() === 'prod') {
return;
}
$request = $event->getRequest();
if (!$request->isXmlHttpRequest()) {
return;
}
$response = $event->getResponse();
$response->headers->set('Symfony-Debug-Toolbar-Replace', 1);
}
}