首页 > 作文

再谈Yii Framework框架中的事件event原理与应用

更新时间:2023-04-08 16:52:34 阅读: 评论:0

本文实例讲述了yii framework框架中的事件event原理与应用。分享给大家供大家参考,具体如下:

再谈yii framework中的事件event,我写过的关于yii事件event的另一篇文章

yii framework 中事件和行为的区别和应用

假设有类mycomponent,它是继承于ccomponent,通过查看 ccomponent 的 __t() 方法,

public function __t($name,$value){  $tter='t'.$name;  if(method_exists($this,$tter))    return $this->$tter($value);  el if(strncac深圳的大学mp($name,'on',2)===0 && method_exists($this,$name))  {    // duplicating geteventhandlers() here for performance    $name=strtolower($name);    if(!ist($this->_e[$name]))      $this->_e[$name]=new clist;    return $this->_e[$name]->add($value);  }  el if(is_array($this->_m))  {    foreach($this->_m as $object)    {      if($object->getenabled() && (property_exists($object,$name) || $object->cantproperty湘西州民族中学($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)));  el    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',***************************************************///其它tan30度等于多少代码'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')

其它文章推荐:

yii组件的事浙江卫视直播在线观看奔跑吧兄弟件机制分析

本文发布于:2023-04-08 16:52:33,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/2c275f7e4a26b5ede3eaac673abd7f63.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:再谈Yii Framework框架中的事件event原理与应用.doc

本文 PDF 下载地址:再谈Yii Framework框架中的事件event原理与应用.pdf

标签:事件   方法   代码   绑定
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图