Hi David, It should not be firing twice, however you are trying to use the shared event manager the wrong way.
If you want to have a listener fire for EVERY dispatch ever, then just attach to normal event manager on EVENT_DISPATCH in one place. If you attach it in the MyModule it will still fire when you access a controller in the Application module. Is that what you need? Cheers, Michael On 10 May 2013 15:01, David Muir <[email protected]> wrote: > Michael, > > Thanks for the explanation. It does explain the issue I have having. > > So the gist is: > > $sharedEvents->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH > > > means that this even will only be triggered if the context is that > namespace. So the one in Application will fire if the context is > "Application" and the one in MyModule will fire if the context is > "MyModule". > > So as an experiment, in Application I tried attaching a listener to > 'MyModule' instead of __NAMESPACE__ and it work as expected. It only fires > when hitting a controller in MyModule. > > But what if I want to attach something that is triggered in all contexts > on the dispatch event? I tried using the * wildcard, but now the shared > listeners get hit twice: > > shared event listener from application module called > shared event listener from my module called > event listener from application module called > event listener from my module called > shared event listener from application module called > shared event listener from my module called > > If my understanding is correct, this happens because there are two > EventManager involved. First is the Application's EventManager, which first > triggers the shared events, then triggers the local events. Then the > controller itself triggers MvcEvent::EVENT_DISPATCH on its own EventManager > instance when the controller's dispatch() method gets called. This in turn > triggers the shared events a 2nd time. > > I guess I got exactly what I asked for, but wasn't > expecting MvcEvent::EVENT_DISPATCH to be triggered twice. Kind of feels > wrong... > > Cheers, > David > > On 10/05/2013, at 4:46 PM, Michael Gooden <[email protected]> > wrote: > > Hi David, > > Keep in mind that you are working with the Zend\Mvc\Application's > EventManager instance. Try and follow the code execution in the > Application::run() function, to see when and why different events fire. > > I would hazard a guess and say that you are accessing a controller in the > MyModule, and not in the main Application module. If you run a controller > from the Application module, you will then see the 'shared event from > application' message show instead of from mymodule. The reason this is > happening is because the shared event manager listener you are attaching is > listening for the event identifier of __NAMESPACE__. When the dispatch > event gets called against a controller under MyModule, only the listener > tied to the namespace of that controller will be fired. The reason you will > always see both normal event listener messages is because those listeners > are both attached to the dispatch event. > > I feel I am failing at explaining this, can you add me on Skype? > > Cheers, > > Michael Gooden > > > On 10 May 2013 03:41, David Muir <[email protected]> wrote: > >> I'm getting some weird behaviour with the EventManager and >> SharedEventManager when used in the Application module vs other modules. >> >> Application Module: >> >> public function onBootstrap(MvcEvent $e) >> { >> //$e->getApplication()->getServiceManager()->get('translator'); >> $eventManager = $e->getApplication()->getEventManager(); >> $moduleRouteListener = new ModuleRouteListener(); >> $moduleRouteListener->attach($eventManager); >> >> $eventManager->attach(MvcEvent::EVENT_DISPATCH, function ($event){ >> echo 'event listener from application module called' . >> PHP_EOL; >> }); >> >> $sharedEvents = $eventManager->getSharedManager(); >> >> $sharedEvents->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, >> function ($event){ >> echo 'shared event listener from application module called'. >> PHP_EOL; >> }); >> >> } >> >> >> My Module: >> >> public function onBootstrap(\Zend\EventManager\EventInterface $e) >> { >> >> $eventManager = $e->getApplication()->getEventManager(); >> $sharedEvents = $eventManager-> getSharedManager(); >> >> $eventManager->attach(MvcEvent::EVENT_DISPATCH, function ($event){ >> echo 'event listener from my module called'.PHP_EOL; >> }); >> >> $sharedEvents->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, >> function ($event){ >> echo 'shared event listener from my module called'.PHP_EOL; >> }); >> >> } >> >> >> >> outputs: >> >> shared event listener from my module called >> event listener from application module called >> event listener from my module called >> >> >> Can someone explain why the shared event listener attached via the >> Application module never gets called? >> >> Secondary to this, I've seen some examples attach listeners to the shared >> manager, and others attach them to the event manager. When do I use one >> instead of the other? >> >> Cheers, >> David >> >> >> -- >> List: [email protected] >> Info: http://framework.zend.com/archives >> Unsubscribe: [email protected] >> >> >> > >
