Skip to content
Snippets Groups Projects

Use response event

1 file
+ 8
8
Compare changes
  • Side-by-side
  • Inline
@@ -8,10 +8,10 @@ use Drupal\Core\Path\PathMatcherInterface;
use Drupal\redirect_404\RedirectNotFoundStorageInterface;
use Drupal\Core\Path\CurrentPathStack;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
/**
* An EventSubscriber that listens to redirect 404 errors.
@@ -88,20 +88,20 @@ class Redirect404Subscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
$events[KernelEvents::EXCEPTION][] = 'onKernelException';
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = 'onResponse';
return $events;
}
/**
* Logs an exception of 404 Redirect errors.
* Logs response with 404 http code.
*
* @param ExceptionEvent $event
* @param ResponseEvent $event
* Is given by the event dispatcher.
*/
public function onKernelException(ExceptionEvent $event) {
public function onResponse(ResponseEvent $event) {
// Only log page not found (404) errors.
if ($event->getThrowable() instanceof NotFoundHttpException) {
if ($event->getResponse()->getStatusCode() === Response::HTTP_NOT_FOUND) {
$path = $this->currentPath->getPath();
// Ignore paths specified in the redirect settings.
Loading