laxity7/yii2-event-service
最新稳定版本:1.0.1
Composer 安装命令:
composer require laxity7/yii2-event-service
包简介
Yii2 events provide a simple observer pattern implementation, allowing you to subscribe and listen for various events that occur within your application.
README 文档
README
Yii2 events provide a simple observer pattern implementation, allowing you to subscribe and listen for various events that occur within your application.
Install
Install via composer
composer require laxity7/yii2-event-service
How to use
1. Create an event class
It will be an any class that contains event data. You can also use the default Yii2 event class yii\base\Event.
For example:
namespace App\Events; use yii\base\Event; final readonly class PaymentEvent { public function __construct( public float $amount, public string $currency, public string $description, ) { } }
2. Create a listener class
It will be a class that contains a method handle or __invoke that will be called when the event is dispatched. The method must accept an event object as an argument.
For example:
namespace App\Events\listeners; use App\Events\PaymentEvent; final class PaymentListener { //public function __invoke(PaymentEvent $event): void public function handle(PaymentEvent $event): void { Yii::info('Event: ' . get_class($event) . ' Trigger: ' . __METHOD__, __METHOD__); } }
Note: You can also use a closure as a listener. The closure must accept an event object as an argument.
3. Subscribe to the event
Add the following code to your configuration file:
'components' => [ 'eventDispatcher' => [ 'class' => \Laxity7\Yii2\Components\EventServiceProvider::class, 'listen' => [ \App\Events\PaymentEvent::class => [ \App\Events\listeners\PaymentListener::class, // listener class function (\App\Events\PaymentEvent $event) { // closure Yii::info('Event: ' . get_class($event) . ' Trigger: ' . __METHOD__, __METHOD__); }, ], ], ], ],
4. Dispatch the event
use App\Events\PaymentEvent; use Laxity7\Yii2\Components\Event; $event = new PaymentEvent(100, 'USD', 'Payment for goods'); \Yii::$app->eventDispatcher->dispatch($event); // or use the helper Event::dispatch($event);
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-21