You are here

public function Redirect404Subscriber::onKernelException in Redirect 8

Logs an exception of 404 Redirect errors.

Parameters

GetResponseForExceptionEvent $event: Is given by the event dispatcher.

File

modules/redirect_404/src/EventSubscriber/Redirect404Subscriber.php, line 102

Class

Redirect404Subscriber
An EventSubscriber that listens to redirect 404 errors.

Namespace

Drupal\redirect_404\EventSubscriber

Code

public function onKernelException(GetResponseForExceptionEvent $event) {

  // Only log page not found (404) errors.
  if ($event
    ->getException() instanceof NotFoundHttpException) {
    $path = $this->currentPath
      ->getPath();

    // Ignore paths specified in the redirect settings.
    if ($pages = mb_strtolower($this->config
      ->get('pages'))) {

      // Do not trim a trailing slash if that is the complete path.
      $path_to_match = $path === '/' ? $path : rtrim($path, '/');
      if ($this->pathMatcher
        ->matchPath(mb_strtolower($path_to_match), $pages)) {
        return;
      }
    }

    // Allow to store paths with arguments.
    if ($query_string = $this->requestStack
      ->getCurrentRequest()
      ->getQueryString()) {
      $query_string = '?' . $query_string;
    }
    $path .= $query_string;
    $langcode = $this->languageManager
      ->getCurrentLanguage()
      ->getId();

    // Write record.
    $this->redirectStorage
      ->logRequest($path, $langcode);
  }
}