Create and Register to Events

Fire one EventTop of Page

To create one event in your code (controller, library, model), simply add :

// Array of data to send to the Event method
$event_data = array(
'key' => 'value'
);

$result = Event::fire('My_Module.action.success', $event_data);

In this example, we suppose the $event_data array will be processed and returned by the method registered to the event "My_Module.action.success".

Register to EventsTop of Page

For one module called "Demo" :

Create the file /modules/Demo/librairies/demo_events.php :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Demo_Events
{
protected static $ci;

public function __construct()
{
self::$ci =& get_instance();

// Register the Events :
// Event::register(<event_name>, array($this, 'method_name'));
// You can register to as many events as needed
Event::register('Article.save.before', array($this, 'on_article_save_before'));

Event::register('My_Module.action.success', array($this, 'on_module_action_success'));
}


/**
* Called before one article is saved
*
* @param $data array(
* 'base' => Array of data,
* 'lang' => Array of lang data,
* )
*
* @return mixed
*
*/
public function on_article_save_before($data)
{
// Camelize the titles of the article before it is saved, just for fun

self::$ci->load->helper('inflector');

foreach($data['lang'] as &$article)
{
$article['title'] = camelize($article['title']);
}

return $data;
}


public function on_module_action_success($data)
{
// Do something with $data

return $data;
}
}