小编Dan*_*igs的帖子

Symfony 4 自定义事件调度器不工作

我已经按照 Symfony 4.3 文档创建了一个自定义事件、调度它并监听它。跟踪我的控制器的执行,看起来事件调度程序没有找到任何订阅者,我无法弄清楚我做错了什么。

我的事件类非常基础:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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', …
Run Code Online (Sandbox Code Playgroud)

events symfony symfony4

3
推荐指数
1
解决办法
3233
查看次数

标签 统计

events ×1

symfony ×1

symfony4 ×1