您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天就跟大家聊聊有关event事件在Yii Framework框架中的原理是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
假设有类MyComponent,它是继承于CComponent,通过查看 CComponent 的 __set() 方法,
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))); }
第四行可知,我们可以通过 onXXX 来直接设置事件的。
方法一:
直接在main.php里面定义
/*************************************************** 在我们想要的内容的前后出现了这些代码 只是为了说明,我们添加的内容是要放在 这个配置数据的一维里面。 'import'=>array( 'application.models.*', 'application.components.*', 'application.helpers.*', ), 'defaultController'=>'post', ***************************************************/ //其它代码 'import'=>array( 'application.models.*', 'application.components.*', 'application.helpers.*', ), /************** 这才是我们想要添加的代码 **************/ 'onBeginRequest' => array('MyEventHandler', 'MyEventHandlerMethod'), 'defaultController'=>'post', //其它代码
方法二:
//参考自framework/logging/CLogRouter.php的init()方法 Yii::app()->attachEventHandler('onEndRequest',array($this,'processLogs'));
绑定到局部事件处理
随时随地无论在controller还是model里面,只要是CComponent的子类,都可以这样定义,
$myComponent->onClick = $callback;
这里的 $callback 指向了一个有效的 PHP 回调。它可以是一个全局函数也可以是类中的一个方法。
如果是后者,它必须以一个数组的方式提供 : array($object,'methodName')
。
看完上述内容,你们对event事件在Yii Framework框架中的原理是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。