提交订单时,我使用事件订阅者来处理一些操作。
问题我的事件没有被调度,但 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
}
}
编辑
找到的唯一解决方法(感谢@nikserg)是将订阅者注入控制器操作(订阅者具有依赖项)然后将我的订阅者注册为服务以供最终services.yaml
使用$eventDispatcher->addSubscriber($subscriber);
$eventDispatcher->dispatch(new OrderEvent($Order),OrderEvent::ORDER_CREATE);
对于这么简单的任务来说,所有这些东西似乎真的很复杂
编辑2
我找到了另一种方法,我可以在不使用的情况下执行我的订阅者,$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
编辑3
我的 OrderSubscriber 已加载到容器中,那么为什么我应该将其明确设置为正在执行?我不知道发生了什么事
php bin/console debug:container
---------------------------------------- ---------------------------------------
Service ID Class name
---------------------------------------- ----------------------------------------
App\EventSubscriber\OrderSuscriber App\EventSubscriber\OrderSuscriber
编辑 4
如果我OrderSubscriber
明确设置我的容器中有两个实例。为什么 symfony 显式执行一组而不是一组resource: '../src/*'