symfony 5-未调度事件 [英] symfony 5 - event not being dispatched
问题描述
提交订单后,我使用事件订阅者来处理一些操作.
I use an event subscriber to handle some actions when my order form is submitted.
问题是我的活动没有分发,但是symfony能够找到他,因为他告诉我我的OrderEvent :: ORDER_CREATE是孤立的.
Problem my event is not being dispached but symfony is able to find him because he tells me that my OrderEvent::ORDER_CREATE is orphan.
我希望通过 die('Hello you from subscription''s;
)来停止执行,但并非如此.
I excpected that execution was stopped with die('Hello you from subscriber');
but it's not.
控制器
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
);
}
}
OrderEvent
OrderEvent
<?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;
}
}
OrderSubscriber
OrderSubscriber
<?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);
The only workaround found (thx to @nikserg) is to inject subscriber into controller action (subscriber has dependencies) then register my subscriber as service in services.yaml
finaly use $eventDispatcher->addSubscriber($subscriber);
before $eventDispatcher->dispatch(new OrderEvent($Order),OrderEvent::ORDER_CREATE);
对于这么简单的任务,所有这些东西似乎真的很复杂
It seems all that stuff is really complex for a task as simple as that
EDIT2
我发现了另一种无需使用 $ eventDispatcher-> addSubscriber($ subscriber);
且仅使用 $ eventDispatcher-> dispatch(new OrderEvent($ Order));
仅当我在 services.yaml
中将订户配置为服务时,但是为什么symfony在services.yaml中确实需要此信息?认为src/中的所有内容都可以用作服务.
I found an another way I'm able to execute my subscriber without usage of $eventDispatcher->addSubscriber($subscriber);
and only with $eventDispatcher->dispatch(new OrderEvent($Order));
only if I configure my subscriber as service in services.yaml
but why symfony does need this information in services.yaml ? Thought that everything in src/ is avaible to be used as service..
# 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已加载到容器中,为什么我应该将其显式设置为正在执行?我不知道发生了什么事
My OrderSubscriber is loaded into container so why I should set it explicitly to being execute ? I can't figure out what's going on
php bin/console debug:container
---------------------------------------- ---------------------------------------
Service ID Class name
---------------------------------------- ----------------------------------------
App\EventSubscriber\OrderSuscriber App\EventSubscriber\OrderSuscriber
编辑4
如果我明确设置 OrderSubscriber
,则有两个实例进入容器.为什么symfony明确执行一组而不是使用 resource:'../src/*'
If I set my OrderSubscriber
explicitly there is two instances of it into container.
Why symfony execute one set explicitly and not the one set with resource: '../src/*'
推荐答案
首先,我要感谢大家的宝贵时间,让我对我的问题表示歉意,原因是我写了 OrderSuscriber
而不是打字错误 OrderSubscriber
这就是为什么我的容器中有2个服务,以及为什么定义的服务明确起作用的原因.
First I want to thank you all for your time and let me apologize my problem was due to a typo I wrote OrderSuscriber
instead of OrderSubscriber
that's why there was 2 services into my container and why defined service explicitly was working.
这篇关于symfony 5-未调度事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!