@@ -64,20 +64,24 @@ refer to the [Configurations](concept-configurations.md#configuration-format) se
...
@@ -64,20 +64,24 @@ refer to the [Configurations](concept-configurations.md#configuration-format) se
When attaching an event handler, you may provide additional data as the third parameter to [[yii\base\Component::on()]].
When attaching an event handler, you may provide additional data as the third parameter to [[yii\base\Component::on()]].
The data will be made available to the handler when the event is triggered and the handler is called. For example,
The data will be made available to the handler when the event is triggered and the handler is called. For example:
```php
```php
// The following code will display "abc" when the event is triggered
// The following code will display "abc" when the event is triggered
// because $event->data contains the data passed to "on"
// because $event->data contains the data passed as the 3rd argument to "on"
$foo->on(Foo::EVENT_HELLO,function($event){
$foo->on(Foo::EVENT_HELLO,'function_name','abc');
functionfunction_name($event){
echo$event->data;
echo$event->data;
},'abc');
}
```
```
You may attach one or multiple handlers to a single event. When an event is triggered, the attached handlers
Event Handler Order
will be called in the order they are attached to the event. If a handler needs to stop the invocation of the
-------------------
handlers behind it, it may set the [[yii\base\Event::handled]] property of the `$event` parameter to be true,
like the following,
You may attach one or more handlers to a single event. When an event is triggered, the attached handlers
will be called in the order that they were attached to the event. If a handler needs to stop the invocation of the
handlers that follow it, it may set the [[yii\base\Event::handled]] property of the `$event` parameter to be true:
```php
```php
$foo->on(Foo::EVENT_HELLO,function($event){
$foo->on(Foo::EVENT_HELLO,function($event){
...
@@ -87,8 +91,7 @@ $foo->on(Foo::EVENT_HELLO, function ($event) {
...
@@ -87,8 +91,7 @@ $foo->on(Foo::EVENT_HELLO, function ($event) {
By default, a newly attached handler is appended to the existing handler queue for the event.
By default, a newly attached handler is appended to the existing handler queue for the event.
As a result, the handler will be called in the last place when the event is triggered.
As a result, the handler will be called in the last place when the event is triggered.
To insert the new handler at the start of the handler queue so that the handler gets called first, y
To insert the new handler at the start of the handler queue so that the handler gets called first, you may call [[yii\base\Component::on()]], passing false for the fourth parameter `$append`:
ou may call [[yii\base\Component::on()]] by passing the fourth parameter `$append` as false: