Tuesday, July 3, 2012

learn yii : Using Yii events

Most Yii classes are extended from CComponent which allows us to achieve great application
flexibility by using events. An event is a message indicating that the application did something.
We can register several event handlers that will react to certain event types. A handler can get
parameters from an event it works with and react accordingly. Using events allows achieving
great application flexibility.
In this recipe, you will learn how to declare and use both predefined and custom events in
your application.
How to do it...
To declare an event in your CComponent child class, you should add a method with a
name starting with on. For example, if you add the onRegister method, you will get a
corresponding event declared.
Typically, events are used like this:
 Declare an event by adding a corresponding method
 Attach one or multiple event handlers
 The component raises an event by using the CComponent::raiseEvent method
 All subscribed handlers are called automatically
Let's look at how we can attach an event handler to an event. To achieve it, we can use the
CComponent::attachEventHandler method. It accepts the following two parameters:
 $name: Event name
 $handler: Event handler; a standard PHP callback should be used

No comments:

Post a Comment