PHP code example of justcoded / yii2-event-listener

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

    

justcoded / yii2-event-listener example snippets


'components' => [
    'listener' => [
        'class'     => \justcoded\yii2\eventlistener\components\EventListener::class,
        'listeners' => [
        	...
        ],
        'observers' => [
        	...
        ],
    ],
],

    'bootstrap'  => ['log', 'listener'],


namespace app\listeners;

use justcoded\yii2\eventlistener\listeners\Listener;
use yii\base\Event;

/**
 * Class SendUserGreeting
 */
class SendUserGreeting extends Listener
{
	/**
	 * Handle action on event trigger.
	 *
	 * @param Event $event
	 *
	 * @return void
	 */
	public function handle(Event $event)
	{
		/* @var \app\models\User $sender */
		$sender = $event->sender;
		
		// TODO: write your code here, for example, send user greeting email after it was registered or created.
	}
}


'components' => [
    'listener' => [
        'class'     => \justcoded\yii2\eventlistener\components\EventListener::class,
        'listeners' => [
        	\app\models\User::class => [
				\app\models\User::EVENT_AFTER_INSERT => \app\listeners\SendUserGreeting::class,
			],
        ],
    ],
],


namespace app\observers;

use app\controllers\SiteController;
use justcoded\yii2\eventlistener\observers\Observer;
use yii\base\Event;

/**
 * Class UserObserver
 */
class SiteControllerObserver extends Observer
{
	public function events()
	{
		return [
			SiteController::EVENT_BEFORE_ACTION => 'before',
			SiteController::EVENT_AFTER_ACTION => 'after',
		];
	}

	/**
	 * Handle before action event
	 *
	 * @param Event $event
	 *
	 * @return void
	 */
	public function before(Event $event)
	{
		/* @var SiteController $sender */
		$sender = $event->sender;
		
		// TODO: write your code here.
	}

	/**
	 * Handle after action event
	 *
	 * @param Event $event
	 *
	 * @return void
	 */
	public function after(Event $event)
	{
		/* @var SiteController $sender */
		$sender = $event->sender;
		
		// TODO: write your code here.
	}	
}

'components' => [
    'listener' => [
        'class'     => \justcoded\yii2\eventlistener\components\EventListener::class,
        'observers' => [
        	app\controllers\SiteController::class => \app\observers\SiteControllerObserver::class,
        	app\models\User::class => \app\observers\UserObserver::class,
        ],
    ],
],


namespace app\observers;

use justcoded\yii2\eventlistener\observers\ActiveRecordObserver;
use yii\db\AfterSaveEvent;

/**
 * Class UserObserver
 */
class UserObserver extends ActiveRecordObserver
{
	/**
	 * Handle AFTER_UPDATE ActiveRecord event.
	 *
	 * @param AfterSaveEvent $event
	 *
	 * @return void
	 */
	public function updated(AfterSaveEvent $event)
	{
		/* @var \app\models\User $sender */
		$sender = $event->sender;
		
		// TODO: write your code here.
	}
}