You are here

public function BrokenLinkSubscriber::handleBrokenLink in Broken Link 8

Same name and namespace in other branches
  1. 8.3 src/EventSubscriber/BrokenLinkSubscriber.php \Drupal\broken_link\EventSubscriber\BrokenLinkSubscriber::handleBrokenLink()
  2. 8.2 src/EventSubscriber/BrokenLinkSubscriber.php \Drupal\broken_link\EventSubscriber\BrokenLinkSubscriber::handleBrokenLink()

Method is called whenever the kernel.exception event is dispatched.

Logs the broken link hit rate. And redirect to path configured, if broken link matches any pattern defined.

Parameters

Event $event: Event object.

File

src/EventSubscriber/BrokenLinkSubscriber.php, line 44

Class

BrokenLinkSubscriber
Class BrokenLinkSubscriber.

Namespace

Drupal\broken_link\EventSubscriber

Code

public function handleBrokenLink(Event $event) {
  $exception = $event
    ->getException();
  if ($exception instanceof NotFoundHttpException) {
    $request_path = rtrim($event
      ->getRequest()
      ->getPathInfo(), '/');
    $broken_link = new BrokenLink([], 'broken_link');
    $broken_link
      ->merge($request_path);
    $broken_link_redirect = new BrokenLinkRedirect([], 'broken_link_redirect');
    $redirect_path = $broken_link_redirect
      ->getRedirectLink($request_path);
    if ($redirect_path) {
      $event
        ->setResponse(new RedirectResponse($redirect_path));
    }
  }
}