Php 为什么symfony2不调用我的事件侦听器?
我有一个有两个包的程序。其中一个(CommonBundle)发送一个事件“common.add_channel”,而另一个(FetcherBundle)上的服务应该正在侦听它。在探查器上,我可以在“未调用侦听器”部分中看到事件common.add_通道。我不明白为什么symfony没有注册我的听众 这是我的操作,在Php 为什么symfony2不调用我的事件侦听器?,php,symfony,Php,Symfony,我有一个有两个包的程序。其中一个(CommonBundle)发送一个事件“common.add_channel”,而另一个(FetcherBundle)上的服务应该正在侦听它。在探查器上,我可以在“未调用侦听器”部分中看到事件common.add_通道。我不明白为什么symfony没有注册我的听众 这是我的操作,在CommonBundle\Controller\ChannelController::createAction中: $dispatcher = new EventDispatcher(
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;
}
}
我做错了什么?为什么在我分派common.add_channel时symfony不调用事件侦听器?新的事件分派器不知道关于在另一个分派器上设置的侦听器的任何信息 在控制器中,您需要访问
事件调度器
服务。框架包的编译器传递将所有侦听器附加到此调度程序。要获取服务,请使用控制器#get()
快捷方式:
/。。。
使用Symfony\Bundle\FrameworkBundle\Controller\Controller;
类ChannelController扩展控制器
{
公共函数createAction()
{
$dispatcher=$this->get('event_dispatcher');
// ...
}
}
Standard symfony问题:在services.yml中添加侦听器后是否清除了缓存?是的。如果您在每次点击页面时都使用app_dev.php,那么对services.yml的任何更改都会重新编译到缓存中。不过我已经清理过很多次了。谢谢你,也帮了我
<?php
namespace Naroga\Reader\FetcherBundle\Service;
class FetcherService {
public function onAddChannel(AddChannelEvent $event) {
die("It's here!");
}
}
kernel.listener.add_channel:
class: Naroga\Reader\FetcherBundle\Service\FetcherService
tags:
- { name: kernel.event_listener, event: common.add_channel, method: onAddChannel }