码迷,mamicode.com
首页 > 其他好文 > 详细

yii2学习——事件(event)

时间:2015-04-14 14:18:13      阅读:720      评论:0      收藏:0      [点我收藏+]

标签:

  yii2的事件机制可以让我们将自定义的代码注入到特定的执行点。绑定事件后,一旦事件被触发,自定义代码便会自动执行。

  例如,在发送邮件时,我们可能引发messageSent事件来发送消息。如果我们想跟踪已经成功发送的邮件,只需要简单地将跟踪代码附加到messageSent事件上。

  由此,我们可以看出,事件执行的大致过程:绑定事件>>触发事件。

  所谓绑定事件,其实是指绑定事件处理器——即event handler。

  event handler是事件被触发执行后的回调方法,有四种回调方法:

  一个全局的PHP函数(不带括号),例如 trim

  一个对象,形式为[$object, [‘methodName‘]],注意methodName不带括号。

  一个类的静态方法,形式为[‘ClassName‘, ‘methodName‘],注意methodName不带括号。

  一个匿名函数,形式为function ($event) { ... }。

  事件的格式如下:

  function ($event) {

  // $event is an object of yii\base\Event or a child class

  }

  通过$event参数,时间处理器可以获得当前事件的如下信息:

  event name

  event sender: 触发事件的对象

  custom data: 触发事件提供的参数

  和事件相关的类

  yii\base\Component

  yii\base\Event

  相关方法:on绑定,off解绑,trigger触发。

  注意:yii\base\Object类没有事件处理功能!

  在mvc应用开发中,我们在哪里绑定事件呢?

  事件的绑定

  方式一:

  追踪下yii\web\Controlle,我们发现:

  yii\web\Controller继承自yii\base\Controller

  yii\base\Controller继承自yii\base\Component

  而component是可以进行事件操作的。

  由此可以知道,在任意控制器中可以直接使用$this->on($name, $handler, $data = null, $append = true)来绑定事件。

  方式二:

  追踪下yii\base\Model,我们发现它也继承自component。

  也就是说,只要我们的model继承了yii\base\Model类,我们就可以在model中使用$this->on($name, $handler, $data = null, $append = true)来绑定事件。

  以上部分描述了如何将处理程序附加到实例级上的事件。有些情况,我们可能想对某个类的所有实例绑定一些处理程序,而不是绑定到单个实例上。当然,对每一个实例都进行绑定是不可取的(那多麻烦啊~),这时候可以通过调用yii\base\Event::on()的静态方法,绑定类级处理事件处理器。

  例如:每当在数据库中插入新记录的时候,一个Actiove Record对象可能想触发一个EVENT_AFTER_INSERT事件。为了追踪Active Record进行的每一个插入操作,你可以这样做.

  一旦Active Record的实例或它的某个子类,触发EVENT_AFTER_INSERT事件,该事件处理程序将被调用。在处理程序中,你可以通过$event->sender来获得触发处理器的对象。

  当对象触发事件时,会先调用实例级处理器,然后再调用类级处理器。

  我们可以通过调用静态方法yii\base\Event::trigger()来触发类级事件。一个类级事件与特定的对象无关。所以,只会触发类级事件处理器。

  http://health.people.com.cn/xywy/zxzhe/8487870876.html

  http://health.people.com.cn/xywy/zxzhe/8487877559.html

  http://health.people.com.cn/xywy/zxzhe/8487925362.html

  http://health.people.com.cn/xywy/zxzhe/8479622862.html

  http://health.people.com.cn/xywy/zxzhe/8479625583.html

  http://health.people.com.cn/xywy/zxzhe/8479628073.html

  http://health.people.com.cn/xywy/zxzhe/8479630887.html

  http://health.people.com.cn/xywy/zxzhe/8479633987.html

  http://health.people.com.cn/xywy/zxzhe/8479636747.html

  http://health.people.com.cn/xywy/zxzhe/8479639541.html

  http://health.people.com.cn/xywy/zxzhe/8479642357.html

  http://health.people.com.cn/xywy/zxzhe/8479645766.html

  http://health.people.com.cn/xywy/zxzhe/8479648123.html

  http://health.people.com.cn/xywy/zxzhe/8479651043.html

  http://health.people.com.cn/xywy/zxzhe/8479653421.html

  http://health.people.com.cn/xywy/zxzhe/8479656166.html

  http://health.people.com.cn/xywy/zxzhe/8479658718.html

  http://health.people.com.cn/xywy/zxzhe/8479661355.html

  http://health.people.com.cn/xywy/zxzhe/8479663907.html

  http://health.people.com.cn/xywy/zxzhe/8479666908.html

  http://health.people.com.cn/xywy/zxzhe/8479670571.html

  http://health.people.com.cn/xywy/zxzhe/8479673751.html

  http://health.people.com.cn/xywy/zxzhe/8479676438.html

  http://health.people.com.cn/xywy/zxzhe/8479679173.html

  http://health.people.com.cn/xywy/zxzhe/8479681366.html

  http://health.people.com.cn/xywy/zxzhe/8479683832.html

  http://health.people.com.cn/xywy/zxzhe/8479686768.html

  http://health.people.com.cn/xywy/zxzhe/8479689116.html

  http://health.people.com.cn/xywy/zxzhe/8479691368.html

  http://health.people.com.cn/xywy/zxzhe/8479693787.html

  http://health.people.com.cn/xywy/zxzhe/8479696012.html

  http://health.people.com.cn/xywy/zxzhe/8479698891.html

  http://health.people.com.cn/xywy/zxzhe/8479701687.html

  http://health.people.com.cn/xywy/zxzhe/8479704282.html

  http://health.people.com.cn/xywy/zxzhe/8479707318.html

  http://health.people.com.cn/xywy/zxzhe/8479713591.html

  http://health.people.com.cn/xywy/zxzhe/8505009527.html

  http://health.people.com.cn/xywy/zxzhe/8505012557.html

  http://health.people.com.cn/xywy/zxzhe/8505015712.html

  http://health.people.com.cn/xywy/zxzhe/8505018127.html

  http://health.people.com.cn/xywy/zxzhe/8505024353.html

  http://health.people.com.cn/xywy/zxzhe/8505027671.html

  http://health.people.com.cn/xywy/zxzhe/8505030487.html

  http://health.people.com.cn/xywy/zxzhe/8505033057.html

  http://health.people.com.cn/xywy/zxzhe/8505035423.html

  http://health.people.com.cn/xywy/zxzhe/8505037838.html

  http://health.people.com.cn/xywy/zxzhe/8505041462.html

  http://health.people.com.cn/xywy/zxzhe/8505043853.html

  http://health.people.com.cn/xywy/zxzhe/8505047243.html

  http://health.people.com.cn/xywy/zxzhe/8505049771.html

  http://health.people.com.cn/xywy/zxzhe/8505052271.html

  http://health.people.com.cn/xywy/zxzhe/8505054732.html

  http://health.people.com.cn/xywy/zxzhe/8505056953.html

  http://health.people.com.cn/xywy/zxzhe/8505059162.html

  http://health.people.com.cn/xywy/zxzhe/8505061653.html

  http://health.people.com.cn/xywy/zxzhe/8505063951.html

  http://health.people.com.cn/xywy/zxzhe/8505066478.html

  http://health.people.com.cn/xywy/zxzhe/8505068987.html

  http://health.people.com.cn/xywy/zxzhe/8505071177.html

  http://health.people.com.cn/xywy/zxzhe/8505073457.html

  http://health.people.com.cn/xywy/zxzhe/8505075960.html

  http://health.people.com.cn/xywy/zxzhe/8505078362.html

  http://health.people.com.cn/xywy/zxzhe/8505080662.html

  http://health.people.com.cn/xywy/zxzhe/8505082821.html

  http://health.people.com.cn/xywy/zxzhe/8505085032.html

  http://health.people.com.cn/xywy/zxzhe/8505087351.html

  http://health.people.com.cn/xywy/zxzhe/8505089622.html

  http://health.people.com.cn/xywy/zxzhe/8505092237.html

  http://health.people.com.cn/xywy/zxzhe/8505095971.html

  http://health.people.com.cn/xywy/zxzhe/8505098212.html

  http://health.people.com.cn/xywy/zxzhe/8505100571.html

  http://health.people.com.cn/xywy/zxzhe/8505102722.html

  http://health.people.com.cn/xywy/zxzhe/8505105275.html

  http://health.people.com.cn/xywy/zxzhe/8505107477.html

  http://health.people.com.cn/xywy/zxzhe/8505109962.html

  http://health.people.com.cn/xywy/zxzhe/8505114367.html

  http://health.people.com.cn/xywy/zxzhe/8505117067.html

  http://health.people.com.cn/xywy/zxzhe/8505119451.html

  http://health.people.com.cn/xywy/zxzhe/8505121713.html

  http://health.people.com.cn/xywy/zxzhe/8505123962.html

  http://health.people.com.cn/xywy/zxzhe/8505126081.html

  http://health.people.com.cn/xywy/zxzhe/8505128387.html

  http://health.people.com.cn/xywy/zxzhe/8505131008.html

  http://health.people.com.cn/xywy/zxzhe/8505133273.html

  http://health.people.com.cn/xywy/zxzhe/8505135557.html

  http://health.people.com.cn/xywy/zxzhe/8505137727.html

  http://health.people.com.cn/xywy/zxzhe/8505196738.html

  http://health.people.com.cn/xywy/zxzhe/8496508792.html

  http://health.people.com.cn/xywy/zxzhe/8496511718.html

  http://health.people.com.cn/xywy/zxzhe/8496519455.html

  http://health.people.com.cn/xywy/zxzhe/8496522051.html

  http://health.people.com.cn/xywy/zxzhe/8496524768.html

  http://health.people.com.cn/xywy/zxzhe/8496527966.html

  http://health.people.com.cn/xywy/zxzhe/8496530083.html

  http://health.people.com.cn/xywy/zxzhe/8496532477.html

  http://health.people.com.cn/xywy/zxzhe/8496534359.html

  http://health.people.com.cn/xywy/zxzhe/8496536923.html

  http://health.people.com.cn/xywy/zxzhe/8496539618.html

  http://health.people.com.cn/xywy/zxzhe/8496542036.html

  http://health.people.com.cn/xywy/zxzhe/8496544922.html

  http://health.people.com.cn/xywy/zxzhe/8496547736.html

  http://health.people.com.cn/xywy/zxzhe/8496550411.html

  http://health.people.com.cn/xywy/zxzhe/8496553178.html

  http://health.people.com.cn/xywy/zxzhe/8496555723.html

  http://health.people.com.cn/xywy/zxzhe/8496558551.html

  http://health.people.com.cn/xywy/zxzhe/8496561013.html

  http://health.people.com.cn/xywy/zxzhe/8496563467.html

  http://health.people.com.cn/xywy/zxzhe/8496565898.html

  http://health.people.com.cn/xywy/zxzhe/8496568702.html

  http://health.people.com.cn/xywy/zxzhe/8496571418.html

  http://health.people.com.cn/xywy/zxzhe/8496574432.html

  http://www.lwinfo.com/uzt/list1/202742.html

  http://www.lwinfo.com/uzt/list1/202743.html

  http://www.lwinfo.com/uzt/list1/202744.html

  http://www.lwinfo.com/uzt/list1/202745.html

  http://www.lwinfo.com/uzt/list1/202746.html

  http://www.lwinfo.com/uzt/list1/202779.html

  http://www.lwinfo.com/uzt/list1/202783.html

  http://www.lwinfo.com/uzt/list1/202784.html

  http://www.lwinfo.com/uzt/list1/202785.html

  http://www.lwinfo.com/uzt/list1/202786.html

  http://www.lwinfo.com/uzt/list1/202788.html

  http://www.lwinfo.com/uzt/list1/202821.html

  http://www.lwinfo.com/uzt/list1/202902.html

  http://www.lwinfo.com/uzt/list1/202903.html

  http://www.lwinfo.com/uzt/list1/202904.html

  http://www.lwinfo.com/uzt/list1/202906.html

  http://www.lwinfo.com/uzt/list1/202907.html

  http://www.lwinfo.com/uzt/list1/202908.html

  http://www.lwinfo.com/uzt/list1/202909.html

  http://www.lwinfo.com/uzt/list1/202910.html

  http://www.lwinfo.com/uzt/list1/202912.html

yii2学习——事件(event)

标签:

原文地址:http://www.cnblogs.com/wf911/p/4424722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!