ZF2: Using SharedEventManager with context

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

ZF2: Using SharedEventManager with context

Björn Rylander
I have no problem attaching an event to a wildcard context and triggering
it from a controller. But how do I set the correct context? If I want to be
able to trigger an event only from a specific controller, which is the
identifier I should pass as the context variable? I may be reading the docs
wrong, but they haven't been any help. If you have an example I would
appreciate it.

With regards.

Björn
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Using SharedEventManager with context

richard
The Event Manager you are triggering the event on, say in a specific
controller, has an array of Identifier properties.

These Identifiers would ordinarily be set in
Zend\Mvc\Controller\AbstractController::setEventManager for a
controller.
$events->setIdentifiers(array(
            'Zend\Stdlib\DispatchableInterface',
            __CLASS__,
            get_class($this),
            $this->eventIdentifier,
            substr(get_class($this), 0, strpos(get_class($this), '\\'))
        ));

The class of the controller has been added as an Identifier to the
Controllers Event Manager. (EventManagerAwareInterface on
AbstractController causes Event Manager to be set by an initializer).

The Shared Event Manager allows attaching an event that can only be
triggered by Event Managers with a correct Identifier. From above,
this can include inheritance ('Zend\Stdlib\DispatchableInterface') but
we can use the controllers class to be more specific.

Attach the event to a specific Identifier (the controller)
$eventManager->attach('My\Controller\Class', 'myEvent', function($e) {
die("it worked!");});

so now with an event attached, in your controller you should be able
to trigger the event like $this->eventManager->trigger('myEvent');


On Tue, Oct 1, 2013 at 8:24 AM, Björn Rylander <[hidden email]> wrote:

> I have no problem attaching an event to a wildcard context and triggering
> it from a controller. But how do I set the correct context? If I want to be
> able to trigger an event only from a specific controller, which is the
> identifier I should pass as the context variable? I may be reading the docs
> wrong, but they haven't been any help. If you have an example I would
> appreciate it.
>
> With regards.
>
> Björn

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Using SharedEventManager with context

richard
^ to trigger the event like $this->getEventManager()->trigger('myEvent');

On Tue, Oct 1, 2013 at 9:34 AM, Richard Jennings
<[hidden email]> wrote:

> The Event Manager you are triggering the event on, say in a specific
> controller, has an array of Identifier properties.
>
> These Identifiers would ordinarily be set in
> Zend\Mvc\Controller\AbstractController::setEventManager for a
> controller.
> $events->setIdentifiers(array(
>             'Zend\Stdlib\DispatchableInterface',
>             __CLASS__,
>             get_class($this),
>             $this->eventIdentifier,
>             substr(get_class($this), 0, strpos(get_class($this), '\\'))
>         ));
>
> The class of the controller has been added as an Identifier to the
> Controllers Event Manager. (EventManagerAwareInterface on
> AbstractController causes Event Manager to be set by an initializer).
>
> The Shared Event Manager allows attaching an event that can only be
> triggered by Event Managers with a correct Identifier. From above,
> this can include inheritance ('Zend\Stdlib\DispatchableInterface') but
> we can use the controllers class to be more specific.
>
> Attach the event to a specific Identifier (the controller)
> $eventManager->attach('My\Controller\Class', 'myEvent', function($e) {
> die("it worked!");});
>
> so now with an event attached, in your controller you should be able
> to trigger the event like $this->eventManager->trigger('myEvent');
>
>
> On Tue, Oct 1, 2013 at 8:24 AM, Björn Rylander <[hidden email]> wrote:
>> I have no problem attaching an event to a wildcard context and triggering
>> it from a controller. But how do I set the correct context? If I want to be
>> able to trigger an event only from a specific controller, which is the
>> identifier I should pass as the context variable? I may be reading the docs
>> wrong, but they haven't been any help. If you have an example I would
>> appreciate it.
>>
>> With regards.
>>
>> Björn

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Using SharedEventManager with context

Björn Rylander
Thank you that helped. Dumping the result
from $this->getEventManager()->getIdentifiers() in a controller gave me the
identifiers I was looking for.


2013/10/1 Richard Jennings <[hidden email]>

> ^ to trigger the event like $this->getEventManager()->trigger('myEvent');
>
> On Tue, Oct 1, 2013 at 9:34 AM, Richard Jennings
> <[hidden email]> wrote:
> > The Event Manager you are triggering the event on, say in a specific
> > controller, has an array of Identifier properties.
> >
> > These Identifiers would ordinarily be set in
> > Zend\Mvc\Controller\AbstractController::setEventManager for a
> > controller.
> > $events->setIdentifiers(array(
> >             'Zend\Stdlib\DispatchableInterface',
> >             __CLASS__,
> >             get_class($this),
> >             $this->eventIdentifier,
> >             substr(get_class($this), 0, strpos(get_class($this), '\\'))
> >         ));
> >
> > The class of the controller has been added as an Identifier to the
> > Controllers Event Manager. (EventManagerAwareInterface on
> > AbstractController causes Event Manager to be set by an initializer).
> >
> > The Shared Event Manager allows attaching an event that can only be
> > triggered by Event Managers with a correct Identifier. From above,
> > this can include inheritance ('Zend\Stdlib\DispatchableInterface') but
> > we can use the controllers class to be more specific.
> >
> > Attach the event to a specific Identifier (the controller)
> > $eventManager->attach('My\Controller\Class', 'myEvent', function($e) {
> > die("it worked!");});
> >
> > so now with an event attached, in your controller you should be able
> > to trigger the event like $this->eventManager->trigger('myEvent');
> >
> >
> > On Tue, Oct 1, 2013 at 8:24 AM, Björn Rylander <[hidden email]>
> wrote:
> >> I have no problem attaching an event to a wildcard context and
> triggering
> >> it from a controller. But how do I set the correct context? If I want
> to be
> >> able to trigger an event only from a specific controller, which is the
> >> identifier I should pass as the context variable? I may be reading the
> docs
> >> wrong, but they haven't been any help. If you have an example I would
> >> appreciate it.
> >>
> >> With regards.
> >>
> >> Björn
>
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Using SharedEventManager with context

Tom Graham
In reply to this post by Björn Rylander
Unsubscribe


On 1 October 2013 08:24, Björn Rylander <[hidden email]> wrote:

> I have no problem attaching an event to a wildcard context and triggering
> it from a controller. But how do I set the correct context? If I want to be
> able to trigger an event only from a specific controller, which is the
> identifier I should pass as the context variable? I may be reading the docs
> wrong, but they haven't been any help. If you have an example I would
> appreciate it.
>
> With regards.
>
> Björn
>