我需要將 requestStack 注入事件訂閱者,但看起來我做錯了什么(當類被構造時)但我不確定它是什么。
首先我訂閱onLineItemAdded然后我想在有效負載中設定一個新欄位,但是關于 RequestStack $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>
uj5u.com熱心網友回復:
您必須在 XML 中注入請求堆疊:
<?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">
<argument type="service" id="request_stack"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</container>
有關建構式注入如何作業的更多資訊,請參閱Symfony 檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471465.html
