Robert Basic's blog

Archives for October, 2011

A Zend Framework 2 EventManager use case

by Robert Basic on October 19, 2011.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

With Zend Framework 2 beta 1 released yesterday and some free time to spare, I decided to finally try and tackle one of the “scariest” additions to the Zend Framework - the EventManager component. Now, I won’t go into details about this whole event voodoo, Matthew already did that. Twice.

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. Caching. Logging…

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. (Yes, I know. The whole dispatch process is now event driven or whatnot in ZF2, but I need event examples in my application, not in the framework.) I don’t claim I found a perfect example for the events, but I tried.

The problem

One of the most “repetitive” places in my code I found is the save method in my models. Pass an array of data to it, possibly do something with that data, validate it, persist it, maybe do some more data mangling, return true/false to the caller. Over and over again, but just with enough difference between different models that there is actually no double code to pull out to an abstract class or some such.

Say, for example, we have a Post of some sort. It has a title and a slug created from the title. Standard stuff, nothing fancy. For the Post to be valid, it needs to have both the title and the slug set.

Now, without the EventManager, the save method could have a similar flow:

  • call the save method, passing in the data array
  • check if the data array has a slug set, if not, create one from the title
  • validate the data array, to make sure both title and slug are properly set
  • save the post

As I said, pretty standard stuff, so I’ll assume you can imagine that piece of code in your head (read, I’m lazy to write it). The problem: the save method is stuffed with data preparing and validation code.

Using the EventManager

This is where, I hope, the EventManager can help. Call the save method and, just before the persist call, trigger a “pre-save” event and then persist the data. Attach two listeners to this “pre-save” event; the first will do the data preparation, the second will do the validation. There, done. Now the save method doesn’t have that unneeded code around it and can be pulled out to an abstract class, all that is in the event listeners.

Let’s see some code:


// This is the Post object

class Post
    protected $events = null;

    public function events()
        if ($this->events === null) {
            $this->events = new Zend\EventManager\EventManager(__CLASS__);

            $this->events->attach('save', array('SlugifyPost', 'slugify'), 100);
            $this->events->attach('save', array('ValidatePost', 'validate'), 90);

        return $this->events;

    // this method can be pulled out to an abstract model class
    // and reuse it for all the models that extend it
    public function save($data)
        $response = $this->events()->trigger('save', $this, $data);

        echo 'data saved! ' . json_encode($response->last());

I just set a “save” event to be triggered and attached two listeners to that event, the slugify and the validate methods. When the save method gets called, the event is triggered and the EventManager calls our listeners. One fairly important point here is the prepareArgs method call, which prepares the event arguments in such way, that when these arguments (the $data array in this case) are modified in one listener, this modification is reflected for the other listeners, too. If you don’t want to modify the arguments in the listeners, this call can be omitted. As for the rest of the code, it’s explained in Matthew’s posts and in the ZF2 docs.

And here’s how the slugify method modifies the data:


class SlugifyPost
    public function slugify($event)
        $data = $event->getParams();

        $event->setParam('slug', strtolower(str_replace(' ', '-', $data['title'])));

        return $data;

Calling the save method itself remains as it was before introducing the EventManager, which means I could add this to my models without changing the API and not break anything! Genius! Theoretically, anyway…


$post = new Post;

$values = array(
    'title' => 'My post'

try {
} catch(\InvalidArgumentException $e) {
    echo $e->getMessage();

You can find this complete example on Github.


So, what do you think? Does this approach makes sense to you? Do tell. I kinda like the idea, but time will tell in the end, as always.

Happy hackin’!

Tags: eventmanager, events, listeners, zend framework, zf2.
Categories: Development, Programming.

Webkonf 2011 recap

by Robert Basic on October 10, 2011.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

This past weekend the Hungarian web conference was held in the amazing Buda Castle, in Budapest. It was a one day conference, with talks being held on 3 tracks, all in all 21 talk. This was my first time here and I attended as a speaker with a talk on Zend Framework 2 (more on that later).

The conference was great, and, as per the organizers, attended by more than 700 people! The building is quite big, so I didn’t feel like it was too crowded, but there were some complaints about not having enough seats for everyone. I found it weird that there was no proper lunch time set, with only 15-20 minutes left between sessions, so I ended up eating only after 3PM.

The talks I attended were also great, especially the PHP 5.4 introduction talk by Kovács Ferenc, aka Tyr43l, and the MySQL high availability and the InnoDB data recovery talks by Podor István.

As for my talk… Well, that was just plain horror. I got so nervous at the beginning that I completely forgot what was I supposed to say. A completely blank mind. Just standing there, looking at the slides and not knowing what am I even doing there… Completely fubar’d the first part of the presentation. The second part got somewhat better, but my overall feeling is just… Ugh, kill me now, please.

Anyway, I decided to give up on my speaking “career” and just stick to programming.

Happy hackin’!

Tags: conference, talk, webkonf.
Categories: Blablabla, Development.