This article describes how to get started with the ZendFramework2.0 event manager (TheEventManager). This article includes a Quick Start example and EventManager options and methods. For more information, see
This article mainly introduces The Zend Framework 2.0 event manager (The EventManager) Getting Started tutorial. This article includes a Quick Start example and EventManager options and methods. For more information, see
Overview
EventManger is a component designed for the following scenarios:
The Code is as follows:
Implement simple topic/Observer Mode
Achieve face-oriented design
Event-driven architecture
The basic architecture allows you to add and remove listeners for specified events, whether on an instance basis or a shared collection, trigger events, and terminate the execution of listeners.
Quick Start
Generally, you will create an EventManager in a class.
The Code is as follows:
Use Zend \ EventManager \ EventManagerInterface;
Use Zend \ EventManager;
Use Zend \ EventManager \ EventManagerAwareInterface;
Class Foo implements EventManagerAwareInterface
{
Protected $ events;
Public function setEventManager (EventManagerInterface $ events)
{
$ Events-> setIdentifiers (array (
_ CLASS __,
Get_called_class (),
));
$ This-> events = $ events;
Return $ this;
}
Public function getEventManager ()
{
If (null ===$ this-> events ){
$ This-> setEventManager (new EventManager ());
}
Return $ this-> events;
}
}
The above Code allows the user to access the EventManager instance, or reset it with a new instance; if it does not exist, it will be instantiated in inertia when used.
EventManager is only interested in whether it triggers some events. The basic trigger accepts three parameters: the event name, which is usually the current function/method name; context, which is usually the instance of the current object; and parameter, it is usually a parameter provided to the current function/method.
The Code is as follows:
Class Foo
{
//... Assume events definition from abve
Public function bar ($ baz, $ bat = null)
{
$ Params = compact ('baz', 'bat ');
$ This-> getEventManager ()-> trigger (_ FUNCTION __, $ this, $ params );
}
}
In order, the trigger event only cares about whether something has listened to the event. Adds the listener to EventManager, specifying a specified event and the callback to be notified. The callback accepts an Event object, which has an accessor used to obtain the Event name, context, and parameters. Let's add a listener and trigger the event.
The Code is as follows:
Use Zend \ Log \ Factory as LogFactory;
$ Log = LogFactory ($ someConfig );
$ Foo = new Foo ();
$ Foo-> getEventManager ()-> attach ('bar', function ($ e) use ($ log ){
$ Event = $ e-> getName ();
$ Target = get_class ($ e-> getTarget ());
$ Params = json_encode ($ e-> getParams ());
$ Log-> info (sprintf (
'% S called on % s, using params % s ',
$ Event,
$ Target,
$ Params
));
});
// Results in log message:
$ Foo-> bar ('baz', 'bat ');
// Reading: bar called on Foo, using params {"baz": "baz", "bat": "bat "}"
Note that the second parameter of attach () is a valid callback. The example shows an anonymous function to keep the example self-contained. However, you can also use a valid function name, a function object, a string that references a static method, or a callback array with a specified static method or instance method. Again, any PHP callback is valid.
Sometimes you may want to specify that one listener does not have an object instance that creates an EventManager class. Zend Framework implements it through the concept of javasdeventcollection. To put it simply, you can use a well-known javasdeventcollection to inject an independent EventManager instance, and the EventManager instance will query it as an additional listener. The listener added to the destdeventcollection is slightly the same as the normal event manager. The call to attach is identical to the EventManager, but an additional parameter is required at the beginning: a specified instance. Remember how to pass an EventManager instance to him _ CLASS? The value, or any string in the array you provide to the constructor, when using a javasdeventcollection, may be used to identify an instance. As an example, suppose we have a zoodeventmanager instance and we know it has been injected into our EventManager instance (for instances, through dependency injection ), we can change the example above to add a shared set:
The Code is as follows:
Use Zend \ Log \ Factory as LogFactory;
// Assume $ events is a Zend \ EventManager \ login deventmanager instance
$ Log = LogFactory ($ someConfig );
$ Events-> attach ('foo', 'bar', function ($ e) use ($ log ){
$ Event = $ e-> getName ();
$ Target = get_class ($ e-> getTarget ());
$ Params = json_encode ($ e-> getParams ());
$ Log-> info (sprintf (
'% S called on % s, using params % s ',
$ Event,
$ Target,
$ Params
));
});
// Later, instantiate Foo:
$ Foo = new Foo ();
$ Foo-> getEventManager ()-> setincludeventcollection ($ events );
// And we can still trigger the above event:
$ Foo-> bar ('baz', 'bat ');
// Results in log message:
// Bar called on Foo, using params {"baz": "baz", "bat": "bat "}"
Note: StaticEventManager