我有以下事件类定义:
use Symfony\Contracts\EventDispatcher\Event;
class CaseEvent extends Event
{
public const NAME = 'case.event';
// ...
}
我创建了一个订阅者,如下所示:
use App\Event\CaseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CaseEventListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [CaseEvent::NAME => 'publish'];
}
public function publish(CaseEvent $event): void
{
// do something
}
}
我还在以下位置定义了以下内容services.yaml
:
App\EventSubscriber\CaseEventListener:
tags:
- { name: kernel.event_listener, event: case.event}
为什么当我调度这样的事件时,监听器方法publish()
永远不会执行?
/**
* Added here for visibility but is initialized in the class constructor
*
* @var EventDispatcherInterface
*/
private $eventDispatcher;
$this->eventDispatcher->dispatch(new CaseEvent($args));
我怀疑问题出在kernel.event_listener
但不确定如何正确地为侦听器订阅事件。