PHP code example of terranc / yii2-observer

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

    

terranc / yii2-observer example snippets


namespace common\models;

use yii\db\ActiveRecord;
use Terran\YiiObserver\ObserverTrait;

class User extends ActiveRecord {
    use ObserverTrait;
}

namespace common\observers;
use common\models\User;

class UserObserver {
    public function creating(User $user) {
        // Your code ...
    }
    public function created(User $user) {
        // Your code ...
    }
    public function updating(User $user) {
        // Your code ...
    }
    public function updated(User $user) {
        // Your code ...
    }
    public function saving(User $user) {
        // Your code ...
    }
    public function saved(User $user) {
        // Your code ...
    }
    public function deleting(User $user) {
        // Your code ...
    }
    public function deleted(User $user) {
        // Your code ...
    }
}


// ...
// Add the following code as needed
\common\models\User::observe(\common\observers\UserObserver::class);
// ...