custom/plugins/TTSeoUrl/src/Service/RequestSubscriber.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TTSeoUrl\Service;
  3. use Shopware\Core\SalesChannelRequest;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Shopware\Storefront\Page\Navigation\Error\ErrorPageLoadedEvent;
  11. class RequestSubscriber implements EventSubscriberInterface
  12. {
  13.     private const ATTRIBUTE_SALES_CHANNEL_BASE_URL 'sw-sales-channel-base-url';
  14.     private const ROUTE_NAME_HOME 'frontend.home.page';
  15.     private const ROUTE_NAME_NAVIGATION 'frontend.navigation.page';
  16.     private SystemConfigService $systemConfigService;
  17.     public function __construct(SystemConfigService $systemConfigService)
  18.     {
  19.         $this->systemConfigService $systemConfigService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             KernelEvents::REQUEST => [
  25.                 ['onRequest'],
  26.             ],
  27.             ErrorPageLoadedEvent::class => 'onErrorPageLoader',
  28.         ];
  29.     }
  30.     /**
  31.      * Summary of onErrorPageLoader
  32.      * [AM-787] 404-Seiten deindexieren
  33.      * @param \Shopware\Storefront\Page\Navigation\Error\ErrorPageLoadedEvent $event
  34.      * @return void
  35.      */
  36.     public function onErrorPageLoader(ErrorPageLoadedEvent $event)
  37.     {
  38.         $page $event->getPage();
  39.         $metaInfomation $page->getMetaInformation();
  40.         $metaInfomation->setRobots('noindex, nofollow');
  41.     }
  42.     public function onRequest(RequestEvent $event): void
  43.     {
  44.         $request $event->getRequest();
  45.         
  46.         if (!$event->isMainRequest()) {
  47.             return;
  48.         }
  49.         if (!$this->isHomeRequest($request) && !$this->isNavigationRequest($request)) {
  50.             return;
  51.         }
  52.         if (!$this->shouldRedirect()) {
  53.             return;
  54.         }
  55.        
  56.         if ($this->isHomeRequest($request)) {
  57.             // storefront pointing not on a sub folder
  58.             if ($request->attributes->get(self::ATTRIBUTE_SALES_CHANNEL_BASE_URL) == '') {
  59.                 return;
  60.             }
  61.             // storefront pointing on a sub folder
  62.             if (!$request->attributes->has(SalesChannelRequest::ATTRIBUTE_STOREFRONT_URL)) {
  63.                 return;
  64.             }
  65.             $canonical $request->attributes->get(SalesChannelRequest::ATTRIBUTE_STOREFRONT_URL);
  66.             $current $request->attributes->get(RequestTransformer::ATTRIBUTE_CURRENT_LINK);
  67.         } elseif ($this->isNavigationRequest($request)) {
  68.             if (!$request->attributes->has(SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK)) {
  69.                 return;
  70.             }
  71.             $canonical $request->attributes->get(SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK);
  72.             $current $request->attributes->get(RequestTransformer::ATTRIBUTE_CURRENT_LINK);
  73.         } else {
  74.             return;
  75.         }
  76.         if ($canonical === $current) {
  77.             // no redirect needed
  78.             return;
  79.         }
  80.         if ($request->getQueryString()) {
  81.             $canonical .= '?' $request->getQueryString();
  82.         }
  83.         $statusCode $this->getStatusCode();
  84.         $event->setResponse(new RedirectResponse($canonical$statusCode));
  85.     }
  86.     private function isHomeRequest(Request $request): bool
  87.     {
  88.         return $request->attributes->get('_route') == self::ROUTE_NAME_HOME;
  89.     }
  90.     private function isNavigationRequest(Request $request): bool
  91.     {
  92.         return $request->attributes->get('_route') == self::ROUTE_NAME_NAVIGATION;
  93.     }
  94.     private function shouldRedirect(): bool
  95.     {
  96.         return $this->systemConfigService->getString('TTSeoUrl.config.method') === 'redirect';
  97.     }
  98.     private function getStatusCode(): int
  99.     {
  100.         return $this->systemConfigService->getInt('TTSeoUrl.config.redirectCode') ?: 301;
  101.     }
  102. }