symfony - Symfony2 event subscriber does not call listeners -
i trying set simple event subscription based on example given here - http://symfony.com/doc/master/components/event_dispatcher/introduction.html.
here's event store:
namespace cookbook\inheritancebundle\event; final class eventstore { const event_sample = 'event.sample'; }
here's event subscriber:
namespace cookbook\inheritancebundle\event; use symfony\component\eventdispatcher\eventsubscriberinterface; use symfony\component\eventdispatcher\event; class subscriber implements eventsubscriberinterface { public static function getsubscribedevents() { var_dump('here'); return array( 'event.sample' => array( array('samplemethod1', 10), array('samplemethod2', 5) )); } public function samplemethod1(event $event) { var_dump('method 1'); } public function samplemethod2(event $event) { var_dump('method 2'); } }
here's config in services.yml:
kernel.subscriber.subscriber: class: cookbook\inheritancebundle\event\subscriber tags: - {name:kernel.event_subscriber}
and here's how raise event:
use symfony\component\eventdispatcher\eventdispatcher; use cookbook\inheritancebundle\event\eventstore; $dispatcher = new eventdispatcher(); $dispatcher->dispatch(eventstore::event_sample);
expected output:
string 'here' (length=4) string 'method 1' (length=8) string 'method 2' (length=8)
actual output:
string 'here' (length=4)
for reason, listener methods don't called. knows what's wrong code? thanks.
you might try inject configured eventdispatcher
(@event_dispatcher
) instead of instanciating new 1 (new eventdispatcher
)
if create , add event-listener symfony still has no reference newly created eventdispatcher
object , won't use it.
if inside controller extends containeraware :
use symfony\component\eventdispatcher\eventdispatcher; use cookbook\inheritancebundle\event\eventstore; ... $dispatcher = $this->getcontainer()->get('event_dispatcher'); $dispatcher->dispatch(eventstore::event_sample);
i've adapted answer this question's answer though context of both questions different, answer still applies.
Comments
Post a Comment