terranc/yii2-observer

This is my package yii2-observer

Fund package maintenance!
terranc

1.3.4 2024-12-16 07:18 UTC

This package is auto-updated.

Last update: 2024-12-16 07:19:21 UTC


README

This is a package that allows you to write code in Yii2 using the same habits as Laravel Observer.

Installation

You can install the package via composer:

composer require terranc/yii2-observer

Usage

Step 1

namespace common\models;

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

class User extends ActiveRecord {
    use ObserverTrait;
}

Step 2

Add File: common/observers/UserObserver.php

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 ...
    }
}

Step 3

Modify common/config/bootstrap.php:

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

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.