我有一個包含兩個程序的程序。其中一個(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