PHP code example of yiicod / yii2-listener

1. Go to this page and download the library: Download yiicod/yii2-listener 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/ */

    

yiicod / yii2-listener example snippets


'bootstrap' => ['listener'],
'components' => [
    'listener' => [
        'class' => 'yiicod\listener\components\Listener'
    ],
]

'controllerMap' => [
    'listener' => [
        'class' => \yiicod\listener\commands\Listener::class
    ],
]

namespace frontend\observers\listeners;

use yii\db\ActiveRecord;
use yiicod\listener\components\listeners\ListenerAbstract;

class TestListener extends ListenerAbstract
{
    /**
     * Call on event method
     */
    public function handle($event)
    {
        // TODO: Implement handle() method.
    }

    /**
     * Return event name for emit
     * @return string
     */
    public static function event(): string
    {
        return ActiveRecord::class . '@' . ActiveRecord::EVENT_AFTER_FIND;
    }
}
 

namespace frontend\observers\subscribers;

use yii\db\ActiveRecord;
use yiicod\listener\components\listeners\SubscriberAbstract;

class TestSubscriber extends SubscriberAbstract
{
    /**
     * @return array
     * [
     *      'event_class@event1' => 'on'
     *      'event_class@event2' => 'on'
     * ]
     */
    public static function subscribe(): array
    {
        return [
            ActiveRecord::class . '@' . ActiveRecord::EVENT_BEFORE_INSERT => 'on',
            ActiveRecord::class . '@' . ActiveRecord::EVENT_AFTER_INSERT => 'on'
        ];
    }

    /**
     * Call on event method
     */
    public function on($event)
    {
        // Handle
    }
}
 

php composer.phar