guanguans / yii-event
Elegant usage events in Yii. - 在 Yii 中优雅的使用事件。
Fund package maintenance!
Wechat
Requires
- yiisoft/yii2: ^2.0
Requires (Dev)
- brainmaestro/composer-git-hooks: ^2.8
- friendsofphp/php-cs-fixer: ^2.0 || ^3.0
- mockery/mockery: ^1.3
- phpunit/phpunit: ^7.0 || ^8.0 || ^9.0
- vimeo/psalm: ^3.0 || ^4.0 || ^5.0
README
Elegant usage events in Yii. - 在 Yii 中优雅的使用事件。
Requirement
- Yii > 2.0
Installation
$ composer require guanguans/yii-event --prefer-dist -vvv
Configuration
... 'components' => [ ... 'event' => [ 'class' => \Guanguans\YiiEvent\Event::class, 'listen' => [ \app\events\ExampleEvent::class => [ \app\listeners\ExampleListener::class, ], ], ], ... ], ...
Usage
Create app\events\ExampleEvent.php
namespace app\events; use yii\base\Event; class ExampleEvent extends Event { public $name = 'example'; }
Create app\listeners\ExampleListener.php
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.