On the principle and application of event in Yii framework

Time:2021-1-25

This paper introduces the principle and application of event in Yii framework. The details are as follows:

Let’s talk about the event in the Yii framework. Another article I wrote about Yii event

Difference and application of event and behavior in Yii framework https://www.jb51.net/article/184208.htm

Suppose there is a class mycomponent, which is inherited from ccomponent. Check the__ Set() method,


public function __set($name,$value)
{
  $setter='set'.$name;
  if(method_exists($this,$setter))
    return $this->$setter($value);
  else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  {
    // duplicating getEventHandlers() here for performance
    $name=strtolower($name);
    if(!isset($this->_e[$name]))
      $this->_e[$name]=new CList;
    return $this->_e[$name]->add($value);
  }
  else if(is_array($this->_m))
  {
    foreach($this->_m as $object)
    {
      if($object->getEnabled() && (property_exists($object,$name) || $object->canSetProperty($name)))
        return $object->$name=$value;
    }
  }
  if(method_exists($this,'get'.$name))
    throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',
      array('{class}'=>get_class($this), '{property}'=>$name)));
  else
    throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
      array('{class}'=>get_class($this), '{property}'=>$name)));
}

As you can see from the fourth line, we can set the events directly through onxxx.

Binding to global event handling

Method 1

Directly in main.php What’s in it

/***************************************************
These codes appear before and after what we want
Just for the sake of illustration, the content we added is to be placed in the
This is one dimension of the configuration data.
'import'=>array(
  'application.models.*',
  'application.components.*',
  'application.helpers.*',
),
'defaultController'=>'post',
***************************************************/

//Other codes
'import'=>array(
  'application.models.*',
  'application.components.*',
  'application.helpers.*',
),

/**************This is the code we want to add**************/
'onBeginRequest' => array('MyEventHandler', 'MyEventHandlerMethod'),

'defaultController'=>'post',
//Other codes

Method 2

//Reference from framework / logging/ CLogRouter.php Init() method of
Yii::app()->attachEventHandler('onEndRequest',array($this,'processLogs'));

Binding to local event handling

Whenever and wherever it is a subclass of ccomponent in controller or model, it can be defined in this way,


$myComponent->onClick = $callback;

The $callback here points to a valid PHP callback. It can be a global function or a method in a class.

If it is the latter, it must be provided as an array:array($object,'methodName')

Other articles recommended:

Analysis of event mechanism of Yii component https://www.jb51.net/article/184203.htm

For more information about Yii, readers who are interested in it can see the following topics: Yii framework introduction and common skills summary, PHP excellent development framework summary, smart template introduction basic course, PHP object-oriented programming introduction course, PHP string usage summary, PHP + MySQL database operation introduction course and PHP common database operation introduction course Summary of writing skills

I hope this article will be helpful to the PHP Programming Based on Yii framework.