我有一个包含两个程序的程序。其中一个(CommonBundle)发送一个事件“common.add_channel”,而另一个(FetcherBundle)上的服务应该正在监听它。在探查器上,我可以在“未调用的侦听器”部分中看到事件common.add_channel。我不明白为什么symfony没有注册我的听众。为什么symfony2不会调用我的事件侦听器?
这是我的动作,里面CommonBundle\Controller\ChannelController::createAction
:
$dispatcher = new EventDispatcher();
$event = new AddChannelEvent($entity);
$dispatcher->dispatch("common.add_channel", $event);
这是我AddChannelEvent
:
<?php
namespace Naroga\Reader\CommonBundle\Event;
use Symfony\Component\EventDispatcher\Event;
use Naroga\Reader\CommonBundle\Entity\Channel;
class AddChannelEvent extends Event {
protected $_channel;
public function __construct(Channel $channel) {
$this->_channel = $channel;
}
public function getChannel() {
return $this->_channel;
}
}
这应该是我的听众(FetcherService.php):
<?php
namespace Naroga\Reader\FetcherBundle\Service;
class FetcherService {
public function onAddChannel(AddChannelEvent $event) {
die("It's here!");
}
}
这里是我注册我的听众(services.yml)的地方:
kernel.listener.add_channel:
class: Naroga\Reader\FetcherBundle\Service\FetcherService
tags:
- { name: kernel.event_listener, event: common.add_channel, method: onAddChannel }
我在做什么错?为什么当我派遣common.add_channel时symfony不会调用事件监听器?
标准symfony问题:在services.yml中添加监听器之后是否清除了缓存? – 2013-04-05 20:24:08
是的。并非如此,如果您在每个pagehit上使用app_dev.php,那么services.yml的任何更改都会重新编译到缓存。不过,我已经清理了很多次。 – 2013-04-08 17:37:08