eventstriggerszend-framework2eventtriggerevent-triggers

ZF2 events triggering


I have the following situation: I one of the application modules I trigger an event, in the same module I create an event listener which listen to that trigger via the shared event manager system, by this step all works fine, I tried to create more listeners in different modules to that event and they also works fine, all listeners are called.

What I want is to have a system where I can be able to trigger the same event from multiple places. For example, I create a Send Mail module, in which I'll have a listener which will listen to the "sendMail" trigger and will do some actions, is it possible to trigger the same event from different modules?

I tried to trigger the same event from 2 places but the attach method of the shared event takes the first parameter as the id of the caller class, i.e. the id of the class that triggers the event, in such way I can trigger the event only from the specified class. What I want is to trigger that event from as many places as I want. Thank you all very much!


Solution

  • The shared event manager allows attaching to wildcards, so to attach to a foo event no matter it's source you would do this:

    $sharedEventManager->attach('*', 'foo', ...);

    For more details check out the EventManager doc page on wildcards: http://framework.zend.com/manual/current/en/tutorials/tutorial.eventmanager.html#wildcards