Notify your visitors with messages

There are many use cases to mention when you want to notify your visitor: login is successful, the contact form is submitted etcetera. I’ve seen some projects where these messages are hard-coded inside the view or pushed by the controller inside a view. This is not exactly the best option, because it adds some logic from another layer inside your controller.

A software pattern exists which might be very useful for this layer, namely the observer pattern. This pattern is designed for handling (distributed) events or state changes, but notifications towards the end user is also a good use case.

Jurrien Stutterheim provided an implementation of the observer pattern and attached this as a proposal on the Zend wiki. A working copy of Zend_Message is provided in the Zym library which you can download here. This blog post I’ll show how to use the Zym_Message component for notifications.

The main notification component will be a controller action plugin. Before the dispatch loop starts, it attaches itself to Zym_Message for a specific notification call. Each time a call is made with the specified notification type, it appends the message to the body of the response in a formatted string.

So to give some code: In the routeStartup function of the plugin, do something like:

$dispatcher = Zym_Message_Dispatcher::get();
$dispatcher->attach($this, self::PREFIX.'*');

I made a class constant called PREFIX, which is very useful as you’ll see later on. The prefix is appended with an asterisk. Now the prefix is really a prefix, you could append any string to it and the plugin will still fetch your messages.
Attaching the instance to the message dispatcher without further information requires you to create a notify() method which will be called when a message is received:

public function notify(Zym_Message $message)
{
    $this->getResponse()->appendBody($this->_formatMessage($message));
}

This function takes the a Zym_Message and appends it to the body of the response. In between, I’ll format the message inside a protected function. The formatting is completely on your own, but this might look like the following:

protected function _formatMessage($message)
{
    $data = $message->getData();
    return sprintf('<div class="notification">%s</div>',
                   $data['message']);
}

Now your plugin is ready to use! The only thing which lasts is the creation of your messages. The observer pattern and its Zym_Message implementation is very cool, because you can create the notifications anywhere you want. The most logical place is inside a controller action, but you’re not limited to.

To create a notification, it’s simple as this:

Zym_Message_Dispatcher::get()->post(
    Soflomo_Controller_Plugin_Notifications::PREFIX,
    $this,
    array('message' => 'Hello World!')
);

The first argument of the post() function is the type of notification. Here you use the class constant mentioned earlier. The last argument contains the data of the messages. In the action plugin you used the key ‘message’ for the text of the message, so you should provide the text here.

Now you’re done! Easy isn’t it ;)