guanguans/yii-event

Elegant usage events in Yii. - 在 Yii 中优雅的使用事件。

Fund package maintenance!
Wechat

v1.0.6 2022-05-25 07:42 UTC

This package is auto-updated.

Last update: 2024-12-25 12:59:23 UTC


README

Elegant usage events in Yii. - 在 Yii 中优雅的使用事件。

Tests Check & fix styling codecov Latest Stable Version Total Downloads License

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.