Symfony 4 自定义事件调度程序不工作
Symfony 4 Custom Event Dispatcher not Working
我已按照 Symfony 4.3 文档创建自定义事件、发送它并监听它。跟踪我的控制器的执行,看起来事件调度程序没有找到任何订阅者,我无法弄清楚我做错了什么。
我的活动class非常基础:
namespace App\Event;
use Symfony\Contracts\EventDispatcher\Event;
class TestEvent extends Event
{
public const NAME = 'test.event';
protected $data;
public function __construct(string $data)
{
$this->data = $data;
}
public function getData(): string
{
return $this->data;
}
}
我的控制器实例化并调度事件:
class TestController extends AbstractController
{
private $eventDispatcher;
public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* @Route("/event", name="event_test")
*/
public function eventTest()
{
$event = new TestEvent('Event string');
$this->eventDispatcher->dispatch($event);
return $this->render('Test/TestEvent.html.twig', [
'title' => 'Test Event',
]);
}
}
我设置了一个订阅者来监听事件和日志。为了确保订阅者正常工作,我还订阅了一个内核事件(有效):
namespace App\EventSubscriber;
use App\Event\TestEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
class TestSubscriber implements EventSubscriberInterface
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public static function getSubscribedEvents()
{
return [
TestEvent::NAME => [['onTestEvent', 20]],
KernelEvents::REQUEST => 'onKernelRequest',
];
}
public function onTestEvent(TestEvent $event)
{
$this->logger->info('Subscriber says: Found test event');
$this->logger->info('Subscriber says: Test event posted this data {data}', [
'data' => $event->getData(),
]);
}
public function onKernelRequest()
{
$this->logger->info('Got Kernel Request');
}
}
当我访问 /event URL 时,我看到了预期的输出,日志显示了内核请求,但没有显示 test.event 日志。
根据文档,我 运行
php bin/console debug:event-dispatcher test.event
在控制台得到
"test.event" 事件的注册听众
订单可调用优先级
#1 App\EventSubscriber\TestSubscriber::onTestEvent() 20
这告诉我订户已注册,但由于某种原因没有被调用。任何帮助都会很棒!
(顺便说一句,我也尝试了一个事件侦听器,并得到了相同的结果。)
谢谢!
在 symfony 4.3 中,如果你发送这样的事件 $this->eventDispatcher->dispatch($event);
dispather 使用方法 get_class($event)
作为事件名称,所以在你的订阅者中你需要更改
TestEvent::NAME => [['onTestEvent', 20]],
到
TestEvent::class=> [['onTestEvent', 20]],
对于听众使用这个:
App\Event\TestListener:
tags:
- { 'name': 'kernel.event_listener', 'event': 'App\Event\TestEvent', 'method': 'onTestEvent' }
我已按照 Symfony 4.3 文档创建自定义事件、发送它并监听它。跟踪我的控制器的执行,看起来事件调度程序没有找到任何订阅者,我无法弄清楚我做错了什么。
我的活动class非常基础:
namespace App\Event;
use Symfony\Contracts\EventDispatcher\Event;
class TestEvent extends Event
{
public const NAME = 'test.event';
protected $data;
public function __construct(string $data)
{
$this->data = $data;
}
public function getData(): string
{
return $this->data;
}
}
我的控制器实例化并调度事件:
class TestController extends AbstractController
{
private $eventDispatcher;
public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* @Route("/event", name="event_test")
*/
public function eventTest()
{
$event = new TestEvent('Event string');
$this->eventDispatcher->dispatch($event);
return $this->render('Test/TestEvent.html.twig', [
'title' => 'Test Event',
]);
}
}
我设置了一个订阅者来监听事件和日志。为了确保订阅者正常工作,我还订阅了一个内核事件(有效):
namespace App\EventSubscriber;
use App\Event\TestEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
class TestSubscriber implements EventSubscriberInterface
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public static function getSubscribedEvents()
{
return [
TestEvent::NAME => [['onTestEvent', 20]],
KernelEvents::REQUEST => 'onKernelRequest',
];
}
public function onTestEvent(TestEvent $event)
{
$this->logger->info('Subscriber says: Found test event');
$this->logger->info('Subscriber says: Test event posted this data {data}', [
'data' => $event->getData(),
]);
}
public function onKernelRequest()
{
$this->logger->info('Got Kernel Request');
}
}
当我访问 /event URL 时,我看到了预期的输出,日志显示了内核请求,但没有显示 test.event 日志。
根据文档,我 运行
php bin/console debug:event-dispatcher test.event
在控制台得到
"test.event" 事件的注册听众
订单可调用优先级
#1 App\EventSubscriber\TestSubscriber::onTestEvent() 20
这告诉我订户已注册,但由于某种原因没有被调用。任何帮助都会很棒!
(顺便说一句,我也尝试了一个事件侦听器,并得到了相同的结果。)
谢谢!
在 symfony 4.3 中,如果你发送这样的事件 $this->eventDispatcher->dispatch($event);
dispather 使用方法 get_class($event)
作为事件名称,所以在你的订阅者中你需要更改
TestEvent::NAME => [['onTestEvent', 20]],
到
TestEvent::class=> [['onTestEvent', 20]],
对于听众使用这个:
App\Event\TestListener:
tags:
- { 'name': 'kernel.event_listener', 'event': 'App\Event\TestEvent', 'method': 'onTestEvent' }