This is documentation for Kohana v2.3.x.

Table of Contents
StatusDraft
TodoMore examples, proof read

Hooks

Hooks are basically files included at the start of Kohana, to be more specific they are loaded in the Kohana::setup() method. At this time these files are loaded, so their methods can be used

No event has been started yet, the first to be started is system.ready

Hook files should be put in application/hooks or similarly in a module folder.

Configuring hooks

To configure hooks edit config.php in your application/config directory. Look the option like this:

File: config.php

$config['enable_hooks'] = FALSE;

Set $config['enable_hooks'] to TRUE and all files in the hooks directory (application/hooks) will be included and the code will be executed.

Example config.php

//To include all files in application/hooks
$config['enable_hooks'] = TRUE;

Hooks and events

The power of hooks mainly comes from the Events class. Hooks are loaded before any of the events are started. This means you can use a hook to modify an event's callbacks. For example you could load the following file as a hook

Example hooks/power.php

class Power {
 public function Kohana(){
   Event::$data = Event::$data.'<!-- Powered by Kohana-->';
 }
 
}
Event::add('system.display', array('Power', 'Kohana'));

This will add a callback to the the system.display event. The callback is to the method Power::Kohana. Power::Kohana adds a small notice to the bottom of each page (<!– Powered by Kohana–>). It utilizes the Event::$data to manipulate the final output. See Events for details.

Constants

When using hooks you might need to get under the hood of Kohana. When the hooks are loaded these constants are set in index.php