没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\base\Event

继承yii\base\Event » yii\base\BaseObject
实现yii\base\Configurable
子类yii\base\ActionEvent, yii\base\ModelEvent, yii\base\ViewEvent, yii\base\WidgetEvent, yii\db\AfterSaveEvent, yii\i18n\MissingTranslationEvent, yii\mail\MailEvent, yii\web\UserEvent
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/base/Event.php

Event 是所有事件类的基类。

它封装了与事件关联的参数。 $sender 属性描述了谁触发了该事件。 并且 $handled 属性指示是否处理了事件。 如果事件处理程序将 $handled 设置为 true, 其余的未经处理的处理程序将不再被调用来处理该事件。

此外,在附加事件处理程序时,可以传递额外的数据, 并在调用事件处理程序时通过 $data 属性使用。

有关 Event 的更多详细信息和使用信息,请参阅 事件的指南文章

公共属性

隐藏继承的属性

属性类型描述被定义在
$data mixed The data that is passed to yii\base\Component::on() when attaching an event handler. yii\base\Event
$handled boolean Whether the event is handled. yii\base\Event
$name string The event name. yii\base\Event
$sender object The sender of this event. yii\base\Event

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
hasHandlers() Returns a value indicating whether there is any handler attached to the specified class-level event. yii\base\Event
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
off() 从类级事件中卸载事件处理程序。 yii\base\Event
offAll() 卸载所有已注册的类级事件处理程序。 yii\base\Event
on() Attaches an event handler to a class-level event. yii\base\Event
trigger() 触发类级事件。 This method will cause invocation of event handlers that are attached to the named event for the specified class and all its parent classes. yii\base\Event

属性详情

$data 公共 属性

The data that is passed to yii\base\Component::on() when attaching an event handler. Note that this varies according to which event handler is currently executing.

public mixed $data null
$handled 公共 属性

Whether the event is handled. Defaults to false. When a handler sets this to be true, the event processing will stop and ignore the rest of the uninvoked event handlers.

public boolean $handled false
$name 公共 属性

The event name. This property is set by yii\base\Component::trigger() and trigger(). Event handlers may use this property to check what event it is handling.

public string $name null
$sender 公共 属性

The sender of this event. If not set, this property will be set as the object whose trigger() method is called. This property may also be a null when this event is a class-level event which is triggered in a static context.

public object $sender null

方法详情

hasHandlers() 公共 静态 方法

Returns a value indicating whether there is any handler attached to the specified class-level event.

Note that this method will also check all parent classes to see if there is any handler attached to the named event.

public static boolean hasHandlers($class, $name)
$class string|object

The object or the fully qualified class name specifying the class-level event.

$name string

The event name.

return boolean

Whether there is any handler attached to the event.

off() 公共 静态 方法

从类级事件中卸载事件处理程序。

This method is the opposite of on().

Note: in case wildcard pattern is passed for class name or event name, only the handlers registered with this wildcard will be removed, while handlers registered with plain names matching this wildcard will remain.

参见 on().

public static boolean off($class, $name, $handler null)
$class string

The fully qualified class name from which the event handler needs to be detached.

$name string

The event name.

$handler callable

The event handler to be removed. If it is null, all handlers attached to the named event will be removed.

return boolean

Whether a handler is found and detached.

offAll() 公共 静态 方法 (自版本 2.0.10 可用)

卸载所有已注册的类级事件处理程序。

参见:

public static void offAll()
on() 公共 静态 方法

Attaches an event handler to a class-level event.

When a class-level event is triggered, event handlers attached to that class and all parent classes will be invoked.

For example, the following code attaches an event handler to ActiveRecord's afterInsert event:

Event::on(ActiveRecord::className(), ActiveRecord::EVENT_AFTER_INSERT, function ($event) {
    Yii::trace(get_class($event->sender) . ' is inserted.');
});

The handler will be invoked for EVERY successful ActiveRecord insertion.

Since 2.0.14 you can specify either class name or event name as a wildcard pattern:

Event::on('app\models\db\*', '*Insert', function ($event) {
    Yii::trace(get_class($event->sender) . ' is inserted.');
});

For more details about how to declare an event handler, please refer to yii\base\Component::on().

参见 off().

public static void on($class, $name, $handler, $data null, $append true)
$class string

The fully qualified class name to which the event handler needs to attach.

$name string

The event name.

$handler callable

The event handler.

$data mixed

The data to be passed to the event handler when the event is triggered. When the event handler is invoked, this data can be accessed via yii\base\Event::$data.

$append boolean

Whether to append new event handler to the end of the existing handler list. If false, the new handler will be inserted at the beginning of the existing handler list.

trigger() 公共 静态 方法

触发类级事件。 This method will cause invocation of event handlers that are attached to the named event for the specified class and all its parent classes.

public static void trigger($class, $name, $event null)
$class string|object

The object or the fully qualified class name specifying the class-level event.

$name string

The event name.

$event yii\base\Event

The event parameter. If not set, a default yii\base\Event object will be created.