Yii2: Component->trigger can't pass a data

Created on 18 Oct 2016  路  2Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

$component = new \yii\base\Component();
$component->on('test', function($event) {
print_r($event->data);
}, ['foo'=>'123']);
$component->trigger('test', new \yii\base\Event(['data'=>['bar'=>'456']]));

What is the expected result?

['foo'=>'123', 'bar'=>'456']

What do you get instead?

['foo'=>'123']

Additional info

I think the developer should can pass an additional parameters via trigger too. Not only via on.

| Q | A |
| --- | --- |
| Yii version | 2.0.9 |
| PHP version | 5.5.9 |
| Operating system | Ubuntu |

Most helpful comment

@klimov-paul but it's a silly behavior!
we should have this ability to pass data when we want to trigger an event

All 2 comments

This is expected. Event::data is always overridden by the values from event handler.
It is clearly documented:
https://github.com/yiisoft/yii2/blob/master/framework/base/Event.php#L45-L49

@klimov-paul but it's a silly behavior!
we should have this ability to pass data when we want to trigger an event

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MUTOgen picture MUTOgen  路  3Comments

psfpro picture psfpro  路  3Comments

AstRonin picture AstRonin  路  3Comments

sobit picture sobit  路  3Comments

schmunk42 picture schmunk42  路  3Comments