Shopware:注射' requestStack'在订户中
我需要在事件订阅者中注入请求储藏,但看起来我在做错事(当班级构造时),但我不确定是什么。
首先,我要订阅在线iitemaded
然后我想在有效负载中设置一个新字段,但是关于请求stack $ requestStack的通过存在错误。我应该添加任何额外的XML配置以使其正常工作吗?
<?php declare(strict_types=1);
namespace Test\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
class TestCartSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
BeforeLineItemAddedEvent::class => 'onLineItemAdded'
];
}
public function onLineItemAdded(BeforeLineItemAddedEvent $event): void
{
// $lineItem = $event->getLineItem();
$items = $this->requestStack->getCurrentRequest()->get('lineItems');
print_r($items);exit;
}
}
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Test\Subscriber\TestCartSubscriber">
<tag name="kernel.event_subscriber"/>
</service>
</services>
</container>
I need to inject the requestStack in an event subscriber but it looks like I'm doing something wrong (when the class gets constructed) but I'm not sure what it is.
First I'm subscribing to onLineItemAdded
then I want to set a new field in the payload but there is an error regarding the passing of RequestStack $requestStack. should be there any extra XML config that I need to add in order for this to work properly?
<?php declare(strict_types=1);
namespace Test\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
class TestCartSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
BeforeLineItemAddedEvent::class => 'onLineItemAdded'
];
}
public function onLineItemAdded(BeforeLineItemAddedEvent $event): void
{
// $lineItem = $event->getLineItem();
$items = $this->requestStack->getCurrentRequest()->get('lineItems');
print_r($items);exit;
}
}
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Test\Subscriber\TestCartSubscriber">
<tag name="kernel.event_subscriber"/>
</service>
</services>
</container>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在XML中注入请求堆栈:
请参阅有关构造仪注入的工作方式的更多信息。
You have to inject the request stack in the XML:
See the Symfony documentation for more information on how the constructor injection works.