currentPath = $current_path; $this->pathMatcher = $path_matcher; $this->requestStack = $request_stack; $this->languageManager = $language_manager; $this->redirectStorage = $redirect_storage; $this->config = $config->get('redirect_404.settings'); } /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events[KernelEvents::EXCEPTION][] = 'onKernelException'; return $events; } /** * Logs an exception of 404 Redirect errors. * * @param GetResponseForExceptionEvent $event * Is given by the event dispatcher. */ 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); } } }