duanqiaoren9975 2016-01-21 12:02
浏览 5
已采纳

调度员不会发送我的事件symfony

I have an ExampleListener class that look like :

<?php

namespace AppBundle\EventListener;

class ExampleListener
{
    public function helloWorld()
    {
        echo "Hello World!";
    }
}

This is my services.yml

services:
    my.event:
        class: AppBundle\EventListener\ExampleListener
        tags:
            - { name: kernel.event_listener, event: my.event, method: helloWorld }

And then from controller i'm trying to dispatch the event.

$dispatcher = new EventDispatcher();
$dispatcher->dispatch('my.event')

I don't have any error but the helloWorld function is never called. My event is up :

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

the result is :

#1      AppBundle\EventListener\ExampleListener::helloWorld()   0

Why dispatcher doens't call the event right? Thanks.

  • 写回答

2条回答 默认 最新

  • doubaben7394 2016-01-21 12:54
    关注

    You have created a new event dispatcher, which is not the same as Symfony uses to register event listeners you define in you container configuration.

    Instead of creating the event dispatcher yourself, use the one that's already defined by Symfony. You can fetch it from the container, for example in a controller:

    $this->container->get('event_dispatcher')->dispatch('my.event');
    

    If you need to dispatch your event from a service, simply pass the event_dispatcher service as one of constructor arguments:

    services:
        my_service:
            class: Foo\MyService
            arguments:
                - @event_dispatcher
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端