You are here

public function GlobalredirectSubscriber::globalredirectDeslash in Global Redirect 8

Detects a url with an ending slash (/) and removes it.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseEvent $event:

File

src/EventSubscriber/GlobalredirectSubscriber.php, line 123
Contains \Drupal\globalredirect\EventSubscriber\GlobalredirectSubscriber.

Class

GlobalredirectSubscriber
KernelEvents::REQUEST subscriber for redirecting q=path/to/page requests.

Namespace

Drupal\globalredirect\EventSubscriber

Code

public function globalredirectDeslash(GetResponseEvent $event) {
  if (!$this->config
    ->get('deslash') || $event
    ->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
    return;
  }
  $path_info = ltrim($event
    ->getRequest()
    ->getPathInfo(), '/');
  if (substr($path_info, -1, 1) === '/') {
    $path_info = trim($path_info, '/');
    try {
      $path_info = $this->aliasManager
        ->getPathByAlias($path_info);

      // Need to add the slash back.
      $this
        ->setResponse($event, Url::fromUri('internal:/' . $path_info));
    } catch (MatchingRouteNotFoundException $e) {

      // Do nothing here as it is not our responsibility to handle this.
    }
  }
}