本文介绍了Symfony 4自定义事件调度程序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已遵循Symfony 4.3文档创建了一个自定义事件,将其分派并监听.跟踪控制器的执行情况,似乎事件调度程序没有找到任何订阅者,而且我无法弄清楚自己在做什么错.

I have followed the Symfony 4.3 docs to create a custom event, dispatch it, and listen for it. Tracing the execution of my controller, it looks like the event dispatcher does not find any subscribers, and I can't figure out what I'm doing wrong.

我的事件类非常基础:

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;
    }
}

我的控制器实例化并调度事件:

My controller instantiates and dispatches the event:

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',
            ]);
    }
}

我设置了一个订户以监听事件并记录日志.为了确保订阅者正常工作,我还订阅了一个内核事件(有效):

I set up a subscriber to listen for the event and log. To make sure the subscriber works, I also subscribed to a Kernel event (that works):

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日志.

When I visit the /event URL, I see the expected output, and the logs show the Kernel request, but they don't show the test.event log.

根据文档,我运行

php bin/console debug:event-dispatcher test.event

在控制台上

订单可调用优先级

#1 App \ EventSubscriber \ TestSubscriber :: onTestEvent()20

#1 App\EventSubscriber\TestSubscriber::onTestEvent() 20

这告诉我订户已注册,但由于某种原因未被调用.任何帮助都太棒了!

That tells me the subscriber is registered, but for some reason it is not being called. Any help would be fantastic!

(顺便说一句,我也尝试了一个事件监听器,并且得到了相同的结果.)

(BTW, I tried with an event listener as well, and got the same results.)

谢谢!

推荐答案

在symfony 4.3中,如果您调度这样的事件$this->eventDispatcher->dispatch($event); Dispather,请使用方法get_class($event)作为事件名称,因此在您的订户中,您需要更改

In symfony 4.3 if you dispatch event like this $this->eventDispatcher->dispatch($event); dispather use method get_class($event) as event name, so in your subscriber you need change

TestEvent::NAME => [['onTestEvent', 20]],

TestEvent::class=> [['onTestEvent', 20]],

对于收听者,请使用此:

For listener use this:

App\Event\TestListener:
        tags:
            - { 'name': 'kernel.event_listener', 'event': 'App\Event\TestEvent', 'method': 'onTestEvent' }

这篇关于Symfony 4自定义事件调度程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:14