ClientErrorResponseSubscriber.php in Drupal 8
File
core/lib/Drupal/Core/EventSubscriber/ClientErrorResponseSubscriber.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponseInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ClientErrorResponseSubscriber implements EventSubscriberInterface {
public function onRespond(FilterResponseEvent $event) {
if (!$event
->isMasterRequest()) {
return;
}
$response = $event
->getResponse();
if (!$response instanceof CacheableResponseInterface) {
return;
}
if ($response
->isClientError()) {
$http_4xx_response_cacheability = new CacheableMetadata();
$http_4xx_response_cacheability
->setCacheTags([
'4xx-response',
]);
$response
->addCacheableDependency($http_4xx_response_cacheability);
}
}
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = [
'onRespond',
10,
];
return $events;
}
}