Yii Framework Forum: Behaviors and events - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Behaviors and events Rate Topic: -----

#1 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 18 January 2010 - 07:27 AM

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.

Can this be done with "events"?
0

#2 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 526
  • Joined: 10-October 08
  • Location:European Union

Posted 18 January 2010 - 07:56 AM

Try to add something like this at the beginning of your method

if($this->hasEventHandler('onBeginYourEvent'))
	$this->raiseEvent('onBeginYourEvent',new CEvent($this));


then use something like

$this->onBeginYourEvent = array(new SomeClass, 'yourEventHandler');


Please note I didn't test it.
Yii user #37
0

#3 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 18 January 2010 - 08:00 AM

View Postekerazha, on 18 January 2010 - 07:56 AM, said:

Try to add something like this at the beginning of your method

if($this->hasEventHandler('onBeginYourEvent'))
	$this->raiseEvent('onBeginYourEvent',new CEvent($this));


then use something like

$this->onBeginYourEvent = array(new SomeClass, 'yourEventHandler');


Please note I didn't test it.


hi ekerazha,

thanks for your reply. In which method should this be done, and what is "onBeginYourEvent"?

I've read all the articles about behaviors and events, but I don't understand the usage of it all. It's a bit confusing :(

EDIT: I want to "extend" the "createUrl" method of my "BaseUrlManager" with an "createUrl" method of the attached "I18nBehavior".
0

#4 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 526
  • Joined: 10-October 08
  • Location:European Union

Posted 18 January 2010 - 08:21 AM

View Postbitmatix, on 18 January 2010 - 08:00 AM, said:

hi ekerazha,

thanks for your reply. In which method should this be done, and what is "onBeginYourEvent"?


At the beginning of createUrl or before you call it. onBeginYourEvent is the name of the event you create.
Yii user #37
0

#5 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 18 January 2010 - 08:45 AM

View Postekerazha, on 18 January 2010 - 08:21 AM, said:

At the beginning of createUrl or before you call it. onBeginYourEvent is the name of the event you create.


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.
0

#6 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 526
  • Joined: 10-October 08
  • Location:European Union

Posted 18 January 2010 - 08:53 AM

View Postbitmatix, on 18 January 2010 - 08:45 AM, said:

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.


Yeah, when you want to trigger the hook, you raise the event and it will invoke the methods you assigned as event handlers. Please note I didn't test this but it is how I think it does work.
Yii user #37
0

#7 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 18 January 2010 - 09:03 AM

View Postekerazha, on 18 January 2010 - 08:53 AM, said:

Yeah, when you want to trigger the hook, you raise the event and it will invoke the methods you assigned as event handlers. Please note I didn't test this but it is how I think it does work.


Ok, this sounds like it has to be something like this (in "BaseUrlManager"):

public function createUrl($route, $params = array(), $ampersand = '&')
{
if ($this->hasEventHandler('onCreateUrl'))
$this->raiseEvent('onCreateUrl', new CEvent($this));

return parent::createUrl($route, $params, $ampersand);
}

but how do i get all the arguments to the "onCreateUrl" method of the "I18nBehavior"?
0

#8 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 526
  • Joined: 10-October 08
  • Location:European Union

Posted 18 January 2010 - 09:08 AM

View Postbitmatix, on 18 January 2010 - 09:03 AM, said:

Ok, this sounds like it has to be something like this (in "BaseUrlManager"):

public function createUrl($route, $params = array(), $ampersand = '&')
{
if ($this->hasEventHandler('onCreateUrl'))
$this->raiseEvent('onCreateUrl', new CEvent($this));

return parent::createUrl($route, $params, $ampersand);
}

Note your "onCreateUrl" is the event name, not the handler name. You have to assign it like

$this->onCreateUrl = array(new SomeClass, 'yourEventHandler');

Quote

but how do i get all the arguments to the "onCreateUrl" method of the "I18nBehavior"?

Well... I should try, I can't go further with suppositions eh eh
Yii user #37
0

#9 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 18 January 2010 - 09:17 AM

View Postekerazha, on 18 January 2010 - 09:08 AM, said:

Note your "onCreateUrl" is the event name, not the handler name. You have to assign it like

$this->onCreateUrl = array(new SomeClass, 'yourEventHandler');


Well... I should try, I can't go further with suppositions eh eh


OMG, too much confusion. "yourEventHandler" is the "onCreateUrl" function in my "I18nBehavior" class, right?
0

#10 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 18 January 2010 - 09:36 AM

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)
{
  CVarDumper::dump($objEvent,100,true);
}

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 = '&')
{
  if ($this->hasEventHandler('onCreateUrl'))
    $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?
0

#11 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 18 January 2010 - 09:49 AM

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.

Addition:
Of course the class that should provide an event needs to extend from CComponent (or its child classes) as it implements the event handling.

This post has been edited by Mike: 18 January 2010 - 09:52 AM

0

#12 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 526
  • Joined: 10-October 08
  • Location:European Union

Posted 18 January 2010 - 09:57 AM

View Postbitmatix, on 18 January 2010 - 09:36 AM, said:

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?


I tried to "compress" the code, if you look at some implementations inside Yii, it uses something like this

	if($this->hasEventHandler('onBeginRequest'))
		$this->onBeginRequest(new CEvent($this));

	public function onBeginRequest($event)
	{
		$this->raiseEvent('onBeginRequest',$event);
	}


I tried to merge the code in order to avoid an extra method but you're saying it is needed, so just go this way.

This post has been edited by ekerazha: 18 January 2010 - 09:58 AM

Yii user #37
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users