<?php
// src/EventSubscriber/LowercaseUrlSubscriber.php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;
class LowercaseUrlSubscriber implements EventSubscriberInterface
{
public function onKernelRequest(RequestEvent $event)
{
if (!$event->isMainRequest()) {
return;
}
$request = $event->getRequest();
$path = $request->getPathInfo();
// Zoznam ciest, ktoré NIKDY nesmieme lowercase-ovať
$excludedPaths = [
'/payment', // Payum tokeny (capture, notify, atď.)
'/api', // API endpointy (Sylius API, UPS, atď.)
'/_partial', // Interné fragmenty Syliusu
'/_profiler',// Symfony profiler (ak je zapnutý)
'/admin', // Administrácia
'/ajax' // Prípadné custom JS volania
];
foreach ($excludedPaths as $excludedPath) {
// Skontrolujeme, či cesta začína na niektorý z vylúčených prefixov
if (str_starts_with($path, $excludedPath)) {
return;
}
}
// Ak cesta obsahuje veľké písmená a nie je vylúčená, presmerujeme na lowercase verziu
if ($path !== strtolower($path)) {
$event->setResponse(
new RedirectResponse(
strtolower($request->getUri()),
301
)
);
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 30],
];
}
}