sbooker / yii2-event-component
Attaches events to all models
This package's canonical repository appears to be gone and the package has been frozen as a result. Email us for help if needed.
Installs: 111
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 4
Type:yii2-extension
pkg:composer/sbooker/yii2-event-component
This package is auto-updated.
Last update: 2021-05-01 00:12:43 UTC
README
Attaches events to all application models in a very simple way. You just list your event handlers in config/_events.php this way: [ 'event\sender\ClassName' => [ 'eventName' => [ 'event\handler\ClassName' => 'methodName' ] ] ];
See example below.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist sbooker/yii2-event-component "*"
or add
"sbooker/yii2-event-component": "*"
to the require section of your composer.json file.
Usage
    Define app component in main config components section like in this example: 
    'components' => [
    ...
          'eventManager'=> [
              'class'     => 'bariew\eventManager\EventManager',
              'events'    => [
                  'app\models\User' => [
                      'afterInsert' => [
                          ['app\models\Email', 'userRegistration'],
                          'eventQueue',
                      ],  
                  ]
              ],
              'handlers' => [
                  'eventQueue' => function (Event $event) {
                      \Yii::$app->eventQueue->push($event);
                  }  
              ]
          ],
    ]
    Explanation: in the example we defined that after creating new User model ('afterInsert')
    Email::userRegistration($event) method will be called.
    Since 1.1.0 you may also not define event manager, but just put _events.php
    into your config folder returning the same 'events' array as in example:
    <?php
    return [
        'app\models\User' => [
            'afterInsert' => [
                ['app\models\Email', 'userRegistration']
            ],
        ]
    ];
    since 1.3.0 handler can also keep additional data and $append boolean as for Event::on() method eg:
    ... [$handlerClassName, $handlerMethodName, ['myData'], false]