YII2 Learning-Events (event)

Source: Internet
Author: User

The Yii2 event mechanism allows us to inject custom code into specific execution points. Once the event is triggered, the custom code executes automatically once the event is bound.

For example, when sending a message, we may raise the Messagesent event to send the message. If we want to track a message that has been successfully sent, simply attach the tracking code to the Messagesent event.

As a result, we can see the approximate process of event execution: binding events >> triggering events.

The so-called binding event, in fact, refers to the binding event handler-the event handler.

The event handler is a callback method that is triggered after the events have been executed, and there are four callback methods:

A global php function (without parentheses), such as Trim

An object in the form of [$object, [' MethodName ']], note methodName without parentheses.

A static method of a class in the form of [' ClassName ', ' methodName '], note methodName without parentheses.

An anonymous function in the form of functions ($event) {...}.

The format of the event is as follows:

function ($event) {

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

}

With the $event parameter, the time processor can obtain the following information about the current event:

Event name

Event Sender: The object that triggered the event

Custom data: The parameters provided by the triggering event

Event-related classes

Yii\base\component

Yii\base\event

Related methods: On binding, off Unbind, trigger trigger.

Note: The Yii\base\object class has no event handling function!

Where do we bind events in MVC application development?

Binding of events

Way One:

Tracking down Yii\web\controlle, we found:

Yii\web\controller inherited from Yii\base\controller

Yii\base\controller inherited from Yii\base\component

The component is capable of doing event operations.

As a result, you can use $this->on ($name, $handler, $data = null, $append = true) directly in any controller to bind the event.

Way two:

Tracking down Yii\base\model, we found it also inherited from component.

In other words, as long as our model inherits the Yii\base\model class, we can use $this->on ($name, $handler, $data = null, $append = TRUE) in the model to bind the event.

The above section describes how to attach handlers to events at the instance level. In some cases, we might want to bind some handlers to all instances of a class, instead of binding to a single instance. Of course, it's not advisable to bind to every instance (that's a lot of trouble), and at this point you can bind class-level processing event handlers by calling the static method of Yii\base\event::on ().

For example, a Actiove record object may want to trigger a Event_after_insert event whenever a new record is inserted into the database. You can do this in order to track every insert operation that the active record makes.

Once an instance of an active record or one of its subclasses triggers the Event_after_insert event, the event handler is called. In the handler, you can get the object that triggers the processor through $event->sender.

When an object fires an event, the instance-level processor is called before the class-level processor is called.

We can trigger class-level events by calling the static method Yii\base\event::trigger (). A class-level event is not related to a particular object. Therefore, only class-level event handlers are triggered.

  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 Learning-Events (event)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.