Radu Pavelco
Radu Pavelco

Reputation: 63

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!

Upvotes: 1

Views: 744

Answers (3)

Adam Lundrigan
Adam Lundrigan

Reputation: 598

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

Upvotes: 2

Crisp
Crisp

Reputation: 11447

If you use the Zend\EventManager\EventManagerAwareTrait or inherit from ZfcBase\EventManager\EventProvider you can supply your own event identifiers to listen to.

If you take a look at the code contained in the EventManagerAwareTrait::setEventManager() method here

https://github.com/zendframework/zf2/blob/master/library/Zend/EventManager/EventManagerAwareTrait.php#L44-L54

and also in ZfcBase\EventManager\EventProvider::setEventManager() here

https://github.com/ZF-Commons/ZfcBase/blob/master/src/ZfcBase/EventManager/EventProvider.php#L26-L37

you'll notice that the method automatically looks for a property named $eventIdentifier, expecting it to be a string or an array of strings, which if present, gets merged with the default identifiers of FQCN and class name.

So basically, you can add your own identifiers by simply adding a property to your event manager aware classes (the ones triggering events)

<?php

namespace Somenamespace; 

use Zend\EventManager\EventManagerAwareInterface;

class EventTriggeringClass implements EventManagerAwareInterface
{
    use \Zend\EventManager\EventManagerAwareTrait;

    protected $eventIdentifier = 'SendMailIdentifier';

}

and

namespace Someothernamespace;

use Zend\EventManager\EventManagerAwareInterface;

class SomeOtherEventTriggeringClass implements EventManagerAwareInterface
{
    use \Zend\EventManager\EventManagerAwareTrait;

    protected $eventIdentifier = 'SendMailIdentifier';

}

Your listeners would now just need to attach to the SendMailIdentifier rather than target the FQCN of any specific class.

Of course, you can also still attach to the FQCN where necessary, since it's one of the identifiers that was merged by the setEventManager method.

Upvotes: 1

mkempf
mkempf

Reputation: 51

Why do you use the shared event manager?

If you create a service for your "operations" you can initialize a new eventmanager where you can attach an event whereever you want to. And ofc pull the trigger from everywhere.

I also would recommend you to attach an event aggregate for easier code review and readability

Upvotes: 0

Related Questions