class BrokenLinkSubscriber in Broken Link 8
Same name and namespace in other branches
- 8.3 src/EventSubscriber/BrokenLinkSubscriber.php \Drupal\broken_link\EventSubscriber\BrokenLinkSubscriber
 - 8.2 src/EventSubscriber/BrokenLinkSubscriber.php \Drupal\broken_link\EventSubscriber\BrokenLinkSubscriber
 
Class BrokenLinkSubscriber.
@package Drupal\broken_link
Hierarchy
- class \Drupal\broken_link\EventSubscriber\BrokenLinkSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
 
Expanded class hierarchy of BrokenLinkSubscriber
1 string reference to 'BrokenLinkSubscriber'
1 service uses BrokenLinkSubscriber
File
- src/
EventSubscriber/ BrokenLinkSubscriber.php, line 17  
Namespace
Drupal\broken_link\EventSubscriberView source
class BrokenLinkSubscriber implements EventSubscriberInterface {
  /**
   * Constructor.
   */
  public function __construct() {
  }
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events['kernel.exception'] = [
      'handleBrokenLink',
    ];
    return $events;
  }
  /**
   * 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.
   *
   * @param Event $event
   *   Event object.
   */
  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));
      }
    }
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            BrokenLinkSubscriber:: | 
                  public static | function | Returns an array of event names this subscriber wants to listen to. | |
| 
            BrokenLinkSubscriber:: | 
                  public | function | Method is called whenever the kernel.exception event is dispatched. | |
| 
            BrokenLinkSubscriber:: | 
                  public | function | Constructor. |