php - Why is symfony2 not calling my event listeners? -
i have program 2 bundles. 1 of them (commonbundle) dispatches event "common.add_channel", while service on other 1 (fetcherbundle) supposed listening it. on profiler, can see event common.add_channel in "not called listeners" section. don't why symfony not registering listener.
this action, inside commonbundle\controller\channelcontroller::createaction
:
$dispatcher = new eventdispatcher(); $event = new addchannelevent($entity); $dispatcher->dispatch("common.add_channel", $event);
this 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; } }
this supposed listener (fetcherservice.php):
<?php namespace naroga\reader\fetcherbundle\service; class fetcherservice { public function onaddchannel(addchannelevent $event) { die("it's here!"); } }
and here's register listener (services.yml):
kernel.listener.add_channel: class: naroga\reader\fetcherbundle\service\fetcherservice tags: - { name: kernel.event_listener, event: common.add_channel, method: onaddchannel }
what doing wrong? why isn't symfony calling event listener when dispatch common.add_channel?
the new event dispatcher doesn't know listeners set on dispatcher.
in controller, need access event_dispatcher
service. compiler pass of framework bundle attached listeners dispatcher. service, use controller#get()
shortcut:
// ... use symfony\bundle\frameworkbundle\controller\controller; class channelcontroller extends controller { public function createaction() { $dispatcher = $this->get('event_dispatcher'); // ... } }
Comments
Post a Comment