I am not quite sure if a hook system and Yii's event handling system is the same.
Regarding to this and this documents you have to attach an event to a class and somewhere in the code you have to raise this event.
But in my understanding a hook system works like this:
class foo {
public function bar()
{
//do something
}
}
And now you define somewhere (depends on framework) a function called beforeFooBar($obj) or afterFooBar($obj) and this function is called before or after function bar() is executed.
Is there something similar in Yii possible? Or is my understanding of hook systems completely wrong?
Edit:
I am asking for a functionality similar to beforeSave() and afterSave(). Because those events you do not need to raise.
I'm sorry but I don't know about "auto hooks" but here is how event handling in Yii works:
This is the a test class which does nothing special, has just a function
test()which outputs some text. It could be anything, like a model, component or whatever:In method
test()we create the event object and pass it to our methodonTest()which raises the event. The event object is a simple class that extends CEvent. We can use it to pass information about the event to the eventhandler:The controller action looks like this:
Note that you can raise the event like so:
I don't know why but you need the method
onTest()even if its empty. Otherwise you will get some exception when you try to attach an event handler to the object. But you can put these "on" methods aside into an abstract class or something so they don't bother you.Also note that you do not need to create your own event classes, you can simply use
CEventdirectly and pass parameters to it:You can also declare events in behaviours but that is a different story.