« »

Oi, Mind your Messages

Tags: , , ,
Posted in Code, CodeIgniter, PHP

Often, I find I need to send notifications to the user (webpage) during server code. This may be a warning, notification, or a message that an action has hapend. If you’re using a MVC framework it’s not practical to exit running code or echo out the message. From a User Interface perspective it is also imperative that the user knows where to look for these notifications and what they mean.

This all lead me to develop a re-usable solution to drop into any project. As all most all of the sites I work on use CodeIgniter now, once again a CI Library was the obvious choice. Oi is notifications management system that, when stripped to its basic behaviour, has two uses.

  • Stores new notifications added by the application
  • Returns all unread notices added by the application

Pretty Simple. Added to that it will also remember notices between page redirects. So if you add a notice and then reach some code that triggers a redirect the notice will be displayed on the next page.

The library is now in its second incarnation. My original version supported three types of notices

  1. Success (Well done, something that was meant to happen has happened)
  2. Message (Nothings gone wrong, nothings gone right. Just wanted to say a little something)
  3. Error (Oh shit, what did you do? Now i’m gonna have to tidy up after you again!)

Each message would be returned as separate elements with a relevant class representing the type. This was pretty sufficient for most applications but there were times when I wanted to add more attributes (classes, titles, ids) or just needed another ‘type’.

So the new version uses the magic php method __call to provide an unlimited number of ‘types’ via the method $oi->add_{type}($string);

e.g.
$this->oi->add_message('Well Done');

or

$this->oi->add_reallylongandimpressivesupertype('Hello');

More examples and documentation available in the code section

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply