Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/EventSubscriber/RedirectLeadingSlashesSubscriber.php
- 9 core/lib/Drupal/Core/EventSubscriber/RedirectLeadingSlashesSubscriber.php
Namespace
Drupal\Core\EventSubscriber
File
core/lib/Drupal/Core/EventSubscriber/RedirectLeadingSlashesSubscriber.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Cache\CacheableRedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RedirectLeadingSlashesSubscriber implements EventSubscriberInterface {
public function redirect(RequestEvent $event) {
$request = $event
->getRequest();
$path = $request
->getPathInfo();
if (str_contains($path, '//')) {
$path = preg_replace('/\\/+/', '/', $path);
$qs = $request
->getQueryString();
if ($qs) {
$qs = '?' . $qs;
}
$event
->setResponse(new CacheableRedirectResponse($request
->getUriForPath($path) . $qs));
}
}
public static function getSubscribedEvents() : array {
$events[KernelEvents::REQUEST][] = [
'redirect',
1000,
];
return $events;
}
}
Classes