yii-event
Elegant usage events in Yii. - 在 Yii 中优雅的使用事件。
Requirement
- Yii > 2.0
Installation
$ composer require guanguans/yii-event -vvv
Configuration
...
'components' => [
...
'event' => [
'class' => \Guanguans\YiiEvent\Event::class,
'listen' => [
\app\events\ExampleEvent::class => [
\app\listeners\ExampleListener::class,
],
],
],
...
],
...
Usage
app\events\ExampleEvent.php
Create namespace app\events;
use yii\base\Event;
class ExampleEvent extends Event
{
public $name = 'example';
}
app\listeners\ExampleListener.php
Create namespace app\listeners;
use Guanguans\YiiEvent\ListenerInterface;
use yii\base\Event;
class ExampleListener implements ListenerInterface
{
public function handle(Event $event)
{
// to do something.
var_export($event->name);
// var_export($event->data);
}
}
Dispatch event
Yii::$app->event->dispatch(new ExampleEvent());
// Yii::$app->event->dispatch(new ExampleEvent(), $data);
// or
event(new ExampleEvent());
// event(new ExampleEvent(), $data);
Output result
'example'
Testing
$ composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.