custom/plugins/SheStorefrontUrls/src/Components/ChannelListener.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace She\StorefrontUrls\Components;
  3. use Shopware\Core\PlatformRequest;
  4. use Shopware\Core\SalesChannelRequest;
  5. use Shopware\Storefront\Framework\Routing\RequestTransformer as ChannelRequestTransformer;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Routing\RouterInterface;
  10. class ChannelListener implements EventSubscriberInterface
  11. {
  12.     private $router;
  13.     public function __construct(RouterInterface $router null)
  14.     {
  15.         $this->router $router;
  16.     }
  17.     public function setRouterContext(RequestEvent $event)
  18.     {
  19.         $request $event->getRequest();
  20.         $channelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  21.         $this->router->getContext()->setParameter('_channel'$channelId);
  22.     }
  23.     public function setCanonicalUrl(RequestEvent $event)
  24.     {
  25.         $request $event->getRequest();
  26.         $attributes $request->attributes;
  27.         if (!$attributes->has(SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK)
  28.             && $attributes->has(ChannelRequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL)
  29.             && !$attributes->get(RequestTransformer::IS_SEO_URL_REQUEST)) {
  30.             $url $attributes->get(ChannelRequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
  31.             $url .= $this->router->generate($attributes->get('_route'), $attributes->get('_route_params'));
  32.             $attributes->set(SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK$url);
  33.         }
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             KernelEvents::REQUEST => [
  39.                 ['setRouterContext'15],
  40.                 ['setCanonicalUrl'5],
  41.             ],
  42.         ];
  43.     }
  44. }