当我提交订单时,我使用事件订阅者来处理一些操作。
问题是我的事件没有被调度,但是 symfony 能够找到他,因为他告诉我我的 OrderEvent::ORDER_CREATE 是孤立的。
我预计执行会停止die('Hello you from subscriber');
但事实并非如此。
控制器
public function commanderPanierAction(Request $request, SelectionWeb $selectionWeb, TableLumineuse $tableLumineuse, EventDispatcherInterface $eventDispatcher)
{
// DO PREVIOUS STUFF
$Order = new Order();
$OrderForm = $this->createForm(OrderForm::class, $Order);
if ($request->isMethod('POST')) {
$OrderForm->handleRequest($request);
if ($OrderForm->isSubmitted() && $OrderForm->isValid()) {
// OrderForm is valid proceed
$eventDispatcher->dispatch(
new OrderEvent($Order),
OrderEvent::ORDER_CREATE
);
}
}
订单事件
<?php
namespace App\Event;
use App\Entity\Order;
use Symfony\Contracts\EventDispatcher\Event;
class OrderEvent extends Event
{
public const ORDER_CREATE = 'order.created';
protected $order;
public function __construct(Order $order)
{
$this->order= $order;
}
public function getOrder(): Order
{
return $this->order;
}
}
订单订阅者
<?php
namespace App\EventSubscriber;
use App\Event\CommandeWebEvent;
use App\Service\SelectionWeb\SelectionWeb;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderSubscriber implements EventSubscriberInterface
{
private $entityManager;
private $selectionWeb;
public function __construct(EntityManagerInterface $entityManager, SelectionWeb $selectionWeb)
{
$this->entityManager = $entityManager;
$this->selectionWeb = $selectionWeb;
}
public static function getSubscribedEvents()
{
return [
OrderEvent::ORDER_CREATE => [
// The higher the number, the earlier the method is called.
['processOrder', 10],
['notifyOrder', -10]
]
];
}
public function processOrder(OrderEvent $event)
{
// TODO
die('Hello you from subscriber');
}
public function notifyOrder(OrderEvent $event)
{
// TODO
}
}
EDIT
找到的唯一解决方法(感谢@nikserg)是将订阅者注入控制器操作(订阅者具有依赖项),然后将我的订阅者注册为服务services.yaml
最终使用$eventDispatcher->addSubscriber($subscriber);
before $eventDispatcher->dispatch(new OrderEvent($Order),OrderEvent::ORDER_CREATE);
对于这么简单的任务来说,所有这些东西似乎都非常复杂
EDIT2
我找到了另一种无需使用即可执行订阅者的方法$eventDispatcher->addSubscriber($subscriber);
并且仅与$eventDispatcher->dispatch(new OrderEvent($Order));
仅当我将订阅者配置为服务时services.yaml
但为什么 symfony 需要在 services.yaml 中提供这些信息?认为 src/ 中的所有内容都可以用作服务..
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
# If I add those It works
App\EventSubscriber\OrderSubscriber:
autowire: true
EDIT3
我的 OrderSubscriber 已加载到容器中,那么为什么我应该将其显式设置为执行?我不明白发生了什么事
php bin/console debug:container
---------------------------------------- ---------------------------------------
Service ID Class name
---------------------------------------- ----------------------------------------
App\EventSubscriber\OrderSuscriber App\EventSubscriber\OrderSuscriber
EDIT 4
如果我设置我的OrderSubscriber
明确地,容器中有两个实例。
为什么 symfony 显式执行一组而不是执行一组resource: '../src/*'