I have a component called "BaseUrlManager" which extends CUrlManager and a behavior called "I18nBehavior" which is attached to my "BaseUrlManager". In my "I18nBehavior" I want to do some extra stuff for the "createUrl" method before the "createUrl" method of "BaseUrlManager" is called.
Sorry, but it’s still unclear to me what the advantage of events is about. Where do I do the “raiseEvent”? All I want to do, I to “hook” from the behavior class into the “createUrl” function of the component class, because I have to add some extra stuff to the “params” array of the “createUrl” function.
Ok, I’ve tried a little around. Seems like there has to be the following function in “BaseUrlManager” (if this function doens’t exist there will be an error):
public function onCreateUrl()
and in the attached "I18nBehavior" I have a function:
public function doParamManipulation($objEvent)
and in the "attach" method of "I18nBehavior" I have to do:
public function attach($objOwner)
$objOwner->attachEventHandler('onCreateUrl', array($this, 'doParamManipulation'));
the usage is then like this:
public function createUrl($route, $params = array(), $ampersand = '&')
$this->raiseEvent('onCreateUrl', new CEvent($this));
return parent::createUrl($route, $params, $ampersand);
do you think this is the right way? If this is correct, it does mean, that I have to create empty "onXYZ" functions in my component class, right?
That’s right. The existence of a method starting with “on” indicates that an event is available from that class. The implementation doesn’t really matter but it’s kind of convention to fire the according event inside that method. Check e.g. the source of CActiveRecord and onBeforeSave for examples.
Of course the class that should provide an event needs to extend from CComponent (or its child classes) as it implements the event handling.