Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Robert Basic:
Events in a Zend Expressive application
Aug 05, 2016 @ 14:40:47

Robert Basic has written up a new post sharing a method he came up with for event handling in a Zend Expressive application. He makes use of Zend's own EventManager component to integrate it with some of his work from a previous post.

Three weeks ago I wrote a post on how to utilize Tactician in a Zend Expressive application. Today I want to expand on that post a little by adding the possibility to trigger and listen to events using the Zend EventManager component.

Using events allows our application to respond to different events that occur during a request.[...] This allows for a better separation of concerns in some cases, because if we take this approach, our code that deals with [the current functionality] doesn’t care any more what happens after that

He then gets into the code, showing how to install the EventManager component and how to create/inject an event manager into a current object (a Command). He then shows how to attach en event to the handler and perform an action when the event is hit. He points out one issue with this kind of setup, however: the need for all dependencies to be created prior to the event being attached. Fortunately the Zend EventManager comes with DI container support making it simpler to access dependencies needed during the firing of the event.

tagged: zendexpressive framework application event eventmanager tutorial overview introduction

Link: https://robertbasic.com/blog/events-in-a-zend-expressive-application/

Kevin Schroeder's Blog:
A little more advanced ZF2 EventManager usage
Apr 24, 2012 @ 13:54:55

Kevin Schroeder has a new post to his his blog with a bit more advanced example of using the Zend Framework 2 EventManager to make global event triggers.

If you look at my previous post on the ZF2 EventManager class you might be tempted to think that you are limited only to listening to events for local instances of the event manager. But no, my friend, you would be wrong. The event manager actually provides access to a sort of global event manager. But it’s more than that. This global event manager allows you to listen in on events for various targets.

He includes an example of how to use the EventManager in a specific namespace that's triggered based on a custom event. He attaches a custom class, "cls2", to a listener on his "ns1/cls1" class.

tagged: zendframework2 eventmanager example tutorial global

Link:

Rob Allen's Blog:
An introduction to ZendEventManager
Apr 23, 2012 @ 14:29:08

Rob Allen has a new post to his blog today introducing you to the ZendEventManager component of the Zend Framework v2, a key part of how this latest version of the framework does its job.

Zend Framework 2's EventManager is a key component of the framework which is used for the core MVC system. The EventManager allows a class to publish events that other objects can listen for and then act when the event occurs. The convention within Zend Framework 2 is that any class that triggers events composing its own EventManager.

He starts with some terminology to get everyone on the same page (listener, event, EventManager) and includes an example of its use in setting up a "PhotoMapper" object showing how to trigger events in the "findById" method. He shows how to listen for a specific event (in his case, a "pre-execute" on the "findById" method) and a method for "short circuiting" the listener based on the response from the "trigger" call. He also touches on the "SharedEventManager" that can be used to add an event across all of your application at the same time.

tagged: introduction zendframework eventmanager trigger event

Link:

Rob Allen's Blog:
A list of ZF2 Events
Mar 16, 2012 @ 14:49:15

In a reference sort of post, Rob Allen has listed out the events that are provided in the Zend Framework 2 "Application" functionality.

Both the Module Manager and the MVC system use the Event Manger extensively in order to provide "hook points" for you to add your own code into the application flow. This is a list of the events triggered by each class during a standard request with the Skeleton Application.

It's broken up into the three main chunks - Module Manager, Bootstrap and Application - with any sub-requests and their sources (like "render", "dispatch" or "response"). You can find out more about ZF2's Event Manager in other posts like this one from Kevin Schroeder or this from Matthew Weier O'Phinney.

tagged: zendframework2 events eventmanager source

Link:

Robert Basic's Blog:
A Zend Framework 2 EventManager use case
Oct 20, 2011 @ 16:05:43

Robert Basic has a new post to his blog today with an use case for Zend Framework 2's event manager to solve a problem he has with "repetitive code" in some of his models.

Basically, this allows us to have one piece of code to trigger an event and to have one or more listeners listening to this event. When the event gets triggered, the listeners are called and then we can do *something*, like caching or logging. Logging or caching. [...] See, that’s my problem. All the event examples stop at logging and caching. Truly there must be some other example for which this event stuff can be used for.

In his example code, he's used the EventManager in one of his models to add listeners to validate the post and "slugify" the post's title for use on the URL. You can find his code on github if you're interested in the full implementation.

tagged: zendframework eventmanager usecase model listener save

Link:

Kevin Schroeder's Blog:
Zend Framework 2 Event Manager
Sep 16, 2011 @ 16:40:47

Kevin Schroeder has a new post today sharing some of his experience with the Zend Framework 2 Event Manager in a simple example of pre- and post-validation hooks in a model.

I got to play with the Event Manager. I did like the plugin functionality in ZF1, but it required some pretty static coding. In some cases, like the front controller plugins, it makes more sense (though this way seems more desirable). [...] It's a ZF1 application, but since (it seems) the event manager is self-contained (and the autoloader works with both ZF1 and ZF2) you can simply paste it into your include_path and BOOM, you have an event manager.

In his case he has a set of models extending a base class and wanted to introduce pre- and post-validation hooks to make it simpler to check the data he was working with. He includes the code for his base model class showing how he implemented the ZF2 EventManager in his ZF1 application. He attaches an event to the password class property and, on update, it automatically updates a temporary password value too.

You can get more information on using the EventManager in this other post from Matthew Weier O'Phinney.

tagged: eventmanager zendframework tutorial validation hook

Link:

Matthew Weier O'Phinney's Blog:
Using the ZF2 EventManager
Sep 13, 2011 @ 14:25:32

Matthew Weier O'Phinney, lead on the Zend Framework project, has a new post to his blog talking about the event manager in the Zend Framework, v2 and how to use it in a refectored version of a previous post.

Earlier this year, I wrote about Aspects, Intercepting Filters, Signal Slots, and Events, in order to compare these similar approaches to handling both asychronous programming as well as handling cross-cutting application concerns in a cohesive way. I took the research I did for that article, and applied it to what was then a "SignalSlot" implementation within Zend Framework 2, and refactored that work into a new "EventManager" component. This article is intended to get you up and running with it.

You'll need to already have an install of the Zend Framework (v2) installed to follow along. He covers some of the basic terminology and the base code the rest of the tutorial's built from - a simple EventManager instance with a trigger on it. He moves on from there showing how to specify targets for the triggers, setting up global static listeners and listener aggregates as well as how to short circuit listener execution. He wraps up the post by applying all of the examples into one simple caching tool that responds to the trigger by either sending back a new instance or pulling the previously generated one from the cache.

tagged: zendframework v2 eventmanager tutorial framework component

Link:


Trending Topics: