Search code examples
shopwareshopware6

How does Shopware handle the cookie path configuration for stores in different subdirectories?


We have two different saleschannels/shops running on the same domain in different subdirectories. Let's say the domain is example.com, then one shop in one saleschannel is accessible with example.com/de, the other one with example.com/en.
We need two different saleschannels, because the catalog and category-tree differs for those two just to name one reason.

I inspected the cookies set from shopware/symfony and observed that on both shops the cookie-path is set to "/". This leads to one not being able to have a valid sessions in both shops simultanously.

Is there a configuration that we are missing? It seems to me that Shopware should set the cookie path by taking the current saleschannel-domain into consideration?


Solution

  • Setting up sales channels in virtual sub-directories is not preferable and prone to issues like that. If you can, you should save yourself the troubles and consider to set up sub-domains for the other sales channels. All you would have to do is point the sub-domains to the same public directory.

    If you absolutely must stick with the path approach, you will have to do some adjustments. You could in theory have a subscriber that changes the session.cookie_path setting on the fly, based on the current base path. (source) I haven't tested it though and even if it works, I personally would still advice against doing it.

    class SetSessionPathSubscriber implements EventSubscriberInterface
    {
        /**
         * @var SessionStorageInterface 
         */
        private $storage;
    
    
        public function __construct(SessionStorageInterface $storage)
        {
            $this->storage = $storage;
        }
    
        public static function getSubscribedEvents(): array
        {
            return [KernelEvents::REQUEST => ['onKernelRequest', 130]];
        }
    
        public function onKernelRequest(RequestEvent $event): void
        {
            if (!$event->isMasterRequest()) {
                return;
            }
    
            if (!$this->storage instanceof NativeSessionStorage) {
                return;
            }
    
            $options = ['cookie_path' => $event->getRequest()->getBasePath() ?: '/'];
            $this->storage->setOptions($options);
        }
    }