PHP code example of guanguans / yii-event

1. Go to this page and download the library: Download guanguans/yii-event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

guanguans / yii-event example snippets


...
'components' => [
    ...
    'event' => [
        'class' => \Guanguans\YiiEvent\Event::class,
        'listen' => [
            \app\events\ExampleEvent::class => [
                \app\listeners\ExampleListener::class,
            ],
        ],
    ],
    ...
],
...

namespace app\events;

use yii\base\Event;

class ExampleEvent extends Event
{
    public $name = 'example';
}

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);
    }
}

Yii::$app->event->dispatch(new ExampleEvent());
// Yii::$app->event->dispatch(new ExampleEvent(), $data);
// or
event(new ExampleEvent());
// event(new ExampleEvent(), $data);

'example'