Shopware 6.5.3.3 会话处理

huangapple go评论84阅读模式
英文:

Shopware 6.5.3.3 Session Handling

问题

我目前正在对一个Shopware 6插件进行完全重构,以适应新版本6.5.3.3。
由于在较新的Shopware 6版本中移除了会话服务,所以插件无法正常工作。

我尝试通过我的插件内的一些事件订阅者来获取会话,但不幸的是我的解决方案并不适用于所有事件。
通过CheckoutConfirmPageLoadedEvent事件,我可以像这样访问我的会话:

$session = $event->getRequest()->getSession();

但在其他事件中,比如CheckoutOrderPlacedEvent,这个解决方案是不可行的。
在没有旧的会话服务的情况下,有没有新的解决方案可以让我在所有的订阅者中访问会话?

文档没有提供帮助,我在Google或Shopware支持中也找不到任何解决方案。

提前感谢你的帮助!

英文:

I'm currently working on a complete rework of a Shopware 6 plugin for the new version 6.5.3.3.
Because the session service was removed in newer versions of Shopware 6, the plugin does not work anymore.

I tried to get the session via some of my event subscribers inside my plugin, but unfortunately my solution does not work with all events.
With the CheckoutConfirmPageLoadedEvent I can access my session like this:

$session = $event->getRequest()->getSession();

In other events like the CheckoutOrderPlacedEvent this solution is not possible.
What is the new workaround without the old session service to access the session out of all of my subscribers?

The documentation does not help and I couldn't get any solutions from Google or the Shopware Support.

Thanks in advance for your help!

答案1

得分: 1

你可以注入request_stack

<service id="SwagBasicExample\Subscriber\MySubscriber">
    <argument type="service" id="request_stack"/>
    <tag name="kernel.event_subscriber"/>
</service>
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class MySubscriber implements EventSubscriberInterface
{
    public function __construct(
        private readonly RequestStack $requestStack
    ) {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
        ];
    }

    public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
    {
        $request = $this->requestStack->getMainRequest();

        if (!$request || !$request->hasSession()) {
            return;
        }

        $session = $request->getSession();
        
        // ...
    }
}

请记住,事件中缺少Request对象可能不是一个疏忽。例如,事件可能在控制台命令的上下文中分派,而根本没有请求。请评估是否可以使用其他方法存储数据,例如作为购物车扩展

英文:

You can inject the request_stack.

&lt;service id=&quot;SwagBasicExample\Subscriber\MySubscriber&quot;&gt;
    &lt;argument type=&quot;service&quot; id=&quot;request_stack&quot;/&gt;
    &lt;tag name=&quot;kernel.event_subscriber&quot;/&gt;
&lt;/service&gt;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class MySubscriber implements EventSubscriberInterface
{
    public function __construct(
        private readonly RequestStack $requestStack
    ) {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            CheckoutOrderPlacedEvent::class =&gt; &#39;onOrderPlaced&#39;,
        ];
    }

    public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
    {
        $request = $this-&gt;requestStack-&gt;getMainRequest();

        if (!$request || !$request-&gt;hasSession()) {
            return;
        }

        $session = $request-&gt;getSession();
        
        // ...
    }
}

Keep in mind though, that a missing Request object in the event might not be an oversight. The events may very well be dispatched in the context of a console command for example, where there simply is no request at all. Evaluate whether you can use other means to store data, e.g. as a cart extension.

huangapple
  • 本文由 发表于 2023年8月8日 21:49:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定