5. Events

Out of the box both Eden and our MVC framework trigger specific events. Based on these events, you can create your own custom response. Events in Eden are for executing sub routines not originally part of an application scope. Emailing when a comment as been made on your post or logging when an error has been thrown would be valid implementations for an event driven design. A list of default events can be found in Figure 1.

Figure 1. Event List
->trigger('init')		// When eden has initialized
->trigger('config')		// When configuration has been set
->trigger('session')	// When session has started
->trigger('request')	// When the request has been set
->trigger('response')	// When the response class has been chosen
->trigger('head')		// Before the head template is rendered
->trigger('body')		// Before the body template is rendered
->trigger('foot')		// Before the foot template is rendered

As an example, we can create an event handler on the front application level. First open config/filters.php and paste Figure 2.

Figure 2. config/filters.php
<?php //-->
return array('Front_Handler');

When that's done, create a new file in the front folder called handler.php and paste Figure 3.

Figure 3. front/handler.php
<?php //-->
class Front_Handler extends Eden_Class {
	/* Constants
	-------------------------------*/
	/* Public Properties
	-------------------------------*/
	/* Protected Properties
	-------------------------------*/
	/* Private Properties
	-------------------------------*/
	/* Magic
	-------------------------------*/
	public function __construct() {
		front()->listen('init')
			->listen('config', 		$this, 'output')
			->listen('session', 	$this, 'output')
			->listen('request', 	$this, 'output')
			->listen('response', 	$this, 'output')
			->listen('head', 		$this, 'output')
			->listen('body', 		$this, 'output')
			->listen('foot', 		$this, 'output');
	}
	
	/* Public Methods
	-------------------------------*/
	public function output($event, $action) {
		echo $action.'<br />';
		return $this;
	}
	
	/* Protected Methods
	-------------------------------*/
	/* Private Methods
	-------------------------------*/
}

If you refresh your website you should see a series of words representing the event that has just happened. From here you can add on any custom actions. Notice in the listen() that argument 2 says $this. This represents the scope of the method found on argument 3. The event class will automatically call $this->output() given these 2 arguments. The event class in general is pretty flexible. To find out more about events check out Events or move on to modeling.


© 2012 Openovate Labs. All rights reserved.