vendor/shopware/core/Framework/Routing/CanonicalRedirectService.php line 32

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Routing;
  3. use Shopware\Core\Framework\Log\Package;
  4. use Shopware\Core\SalesChannelRequest;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. #[Package('core')]
  10. class CanonicalRedirectService
  11. {
  12.     /**
  13.      * @internal
  14.      */
  15.     public function __construct(private readonly SystemConfigService $configService)
  16.     {
  17.     }
  18.     /**
  19.      * getRedirect takes a request processed by the RequestTransformer and checks,
  20.      * whether it points to a SEO-URL which has been superseded. In case the corresponding
  21.      * configuration option is active, it returns a redirect response to indicate, that
  22.      * the request should be redirected to the canonical URL.
  23.      */
  24.     public function getRedirect(Request $request): ?Response
  25.     {
  26.         // This attribute has been set by the RequestTransformer if the requested URL was superseded.
  27.         $canonical $request->attributes->get(SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK);
  28.         $shouldRedirect $this->configService->get('core.seo.redirectToCanonicalUrl');
  29.         if (!$shouldRedirect) {
  30.             return null;
  31.         }
  32.         if (empty($canonical)) {
  33.             return null;
  34.         }
  35.         $queryString $request->getQueryString();
  36.         if ($queryString) {
  37.             $canonical sprintf('%s?%s'$canonical$queryString);
  38.         }
  39.         return new RedirectResponse($canonicalResponse::HTTP_MOVED_PERMANENTLY);
  40.     }
  41. }