Events and hooks in Kohana

Difficulty: Advanced

Some people have problems understanding the events system and in general I think its wise to dedicate a post on the subject since they’re an intricate part of the Kohana core. Together with hooks (but not necessarily) they have great power and can significantly change the way Kohana works. Do know that in order to work with Kohana you don’t need to use events per se (although Kohana uses them for internal use), Kohana will work flawlessly without events. Only when you want to accomplish some complicated things that hack into the core of Kohana you need to use hooks and events. I’ll cover events and hooks in general in this post and hope to cover a more concrete usage example in the future.

Let me first explain how the hooks system works. It’s fairly simple, you enable the hooks setting in ‘config/hooks.php’ and set $config['enable'] to true. Now you place a file in application/hooks and next time you open a page the hook is loaded and the code in the file is executed.

A hook is just a simple php file that’s included early on in the starting up of Kohana. However, you might want the code to be triggered later on in Kohana. This is where events come in. Events are triggered at several different points in the execution of Kohana. You can find an overview of the different events in the user guide.

Triggering an event is done like this and you can see it happening several times in the Kohana core code. The name of an event is freeform although the ‘prefix.name’ convention is used in Kohana
Triggering events

// Run the system.post_controller event
Event::run('system.post_controller');

Any callbacks (a call to a function or a class method) that is associated with the event is then launched.

Adding callbacks
‘hooks/foobar.php’

class Foo{
public function bar(){
echo 'foobar';
}
}
Event::add('system.post_controller', array('Foo','bar'));

When the system.post_controller event is trigger in with the Event::run method, Foo::bar is called in the callback. This code will append ‘foobar’ to you output. You can find alternate ways of adding callbacks in the user guide.

There is also a way of changing data with events. See the following example which has no real use but should be self-explanatory.

$data='some string with no capitals';
Event::add('string.capitalize','capitalize');
Event::run('string.capitalize',$data);
echo $data; //echoes 'some string with capitals: Paris, London, Berlin, Amsterdam, Moscow'
 
function capitalize(){
Event::$data=str_replace(' no','',Event::$data);
Event::$data=Event::$data.': Paris, London, Berlin, Amsterdam, Moscow';
}

So, when a second argument is passed in the Event::run() method, it will be available through Event::$data. It’s passed by reference.

I’ll go into a more concrete example next time.

viagra
free viagra
buy viagra online
generic viagra
how does viagra work
cheap viagra
buy viagra
buy viagra online inurl
viagra 6 free samples
viagra online
viagra for women
viagra side effects
female viagra
natural viagra
online viagra
cheapest viagra prices
herbal viagra
alternative to viagra
buy generic viagra
purchase viagra online
free viagra without prescription
viagra attorneys
free viagra samples before buying
buy generic viagra cheap
viagra uk
generic viagra online
try viagra for free
generic viagra from india
fda approves viagra
free viagra sample
what is better viagra or levitra
discount generic viagra online
viagra cialis levitra
viagra dosage
viagra cheap
viagra on line
best price for viagra
free sample pack of viagra
viagra generic
viagra without prescription
discount viagra
gay viagra
mail order viagra
viagra inurl
generic viagra online paypal
generic viagra overnight
generic viagra online pharmacy
generic viagra uk
buy cheap viagra online uk
suppliers of viagra
how long does viagra last
viagra sex
generic viagra soft tabs
generic viagra 100mg
buy viagra onli
generic viagra online without prescription
viagra energy drink
cheapest uk supplier viagra
viagra cialis
generic viagra safe
viagra professional
viagra sales
viagra free trial pack
viagra lawyers
over the counter viagra
best price for generic viagra
viagra jokes
buying viagra
viagra samples
viagra sample
cialis
generic cialis
cheapest cialis
buy cialis online
buying generic cialis
cialis for order
what are the side effects of cialis
buy generic cialis
what is the generic name for cialis
cheap cialis
cialis online
buy cialis
cialis side effects
how long does cialis last
cialis forum
cialis lawyer ohio
cialis attorneys
cialis attorney columbus
cialis injury lawyer ohio
cialis injury attorney ohio
cialis injury lawyer columbus
prices cialis
cialis lawyers
viagra cialis levitra
cialis lawyer columbus
online generic cialis
daily cialis
cialis injury attorney columbus
cialis attorney ohio
cialis cost
cialis professional
cialis super active
how does cialis work
what does cialis look like
cialis drug
viagra cialis
cialis to buy new zealand
cialis without prescription
free cialis
cialis soft tabs
discount cialis
cialis generic
generic cialis from india
cheap cialis sale online
cialis daily
cialis reviews
cialis generico
how can i take cialis
cheap cialis si
cialis vs viagra
levitra
generic levitra
levitra attorneys
what is better viagra or levitra
viagra cialis levitra
levitra side effects
buy levitra
levitra online
levitra dangers
how does levitra work
levitra lawyers
what is the difference between levitra and viagra
levitra versus viagra
which works better viagra or levitra
buy levitra and overnight shipping
levitra vs viagra
canidan pharmacies levitra
how long does levitra last
viagra cialis levitra
levitra acheter
comprare levitra
levitra ohne rezept
levitra 20mg
levitra senza ricetta
cheapest generic levitra
levitra compra
cheap levitra
levitra overnight
levitra generika
levitra kaufen


4 Responses to “Events and hooks in Kohana”

  1. Lick Says:

    I’m kind of lost in how the default events are triggered. If I recall correctly: ready, routing, execute, shutdown right? But when do the other events like post_controller get triggered?

  2. dlib Says:

    They’re listed in the user guide. The post_controller is triggered in Kohana::instance() just as pre_controller and post_controller_constructor

  3. Ninjapenguin - Home page of Matthew Wells » Blog Archive » Practical Kohana Hooks example: PHPIDS Says:

    [...] further to Dlibs tutorial on hooks he posted up I thought I would help to demonstrate the power of this with a practical example of [...]

  4. Ikke Says:

    What i can’t find anywhere is where to place the Event::add()

    In what i understand, this should be executed before the events are fired.

    In the examples i see, they are just placed below the code that should be executed when the event fires.

    But that code would in normal cases on the time when most events are allready fired. Can anyone cast a light on this?

Leave a Comment