src/EventSubscriber/LowercaseUrlSubscriber.php line 13

Open in your IDE?
  1. <?php
  2. // src/EventSubscriber/LowercaseUrlSubscriber.php
  3. namespace App\EventSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. class LowercaseUrlSubscriber implements EventSubscriberInterface
  9. {
  10.     public function onKernelRequest(RequestEvent $event)
  11.     {
  12.         if (!$event->isMainRequest()) {
  13.             return;
  14.         }
  15.         $request $event->getRequest();
  16.         $path $request->getPathInfo();
  17.         // Zoznam ciest, ktoré NIKDY nesmieme lowercase-ovať
  18.         $excludedPaths = [
  19.             '/payment',  // Payum tokeny (capture, notify, atď.)
  20.             '/api',      // API endpointy (Sylius API, UPS, atď.)
  21.             '/_partial'// Interné fragmenty Syliusu
  22.             '/_profiler',// Symfony profiler (ak je zapnutý)
  23.             '/admin',    // Administrácia
  24.             '/ajax'      // Prípadné custom JS volania
  25.         ];
  26.         foreach ($excludedPaths as $excludedPath) {
  27.             // Skontrolujeme, či cesta začína na niektorý z vylúčených prefixov
  28.             if (str_starts_with($path$excludedPath)) {
  29.                 return;
  30.             }
  31.         }
  32.         // Ak cesta obsahuje veľké písmená a nie je vylúčená, presmerujeme na lowercase verziu
  33.         if ($path !== strtolower($path)) {
  34.             $event->setResponse(
  35.                 new RedirectResponse(
  36.                     strtolower($request->getUri()),
  37.                     301
  38.                 )
  39.             );
  40.         }
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             KernelEvents::REQUEST => ['onKernelRequest'30],
  46.         ];
  47.     }
  48. }