https://laravelacademy.org/post/9713.html

Controller 的逻辑一部分是业务的,一部分就是往 model 里 CUID 了,添加一些 cuid 的回调是绝对有必要的.

订阅者方式重写模型事件

已知模型事件有:

retrieved:获取到模型实例后触发
creating:插入到数据库前触发
created:插入到数据库后触发
updating:更新到数据库前触发
updated:更新到数据库后触发
saving:保存到数据库前触发(插入/更新之前,无论插入还是更新都会触发)
saved:保存到数据库后触发(插入/更新之后,无论插入还是更新都会触发)
deleting:从数据库删除记录前触发
deleted:从数据库删除记录后触发
restoring:恢复软删除记录前触发
restored:恢复软删除记录后触发

给机会让你在 model 的声明周期进行各种 callback.

最简单的就是在 Provider:boot 里直接添加

// app/Providers/EventServiceProvider.php

public function boot()
{
    parent::boot();

    // 监听模型获取事件
    User::retrieved(function ($user) {
        Log::info('从模型中获取用户[' . $user->id . ']:' . $user->name);
    });
}

但是不够优雅?来个观察者模式?

添加 Event 类

app/Events下创建:

php artisan make:event UserCreate

1 添加 User model 进来 by __construct

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

use \App\User;

class UserCreate
{
    use Dispatchable, InteractsWithSockets, SerializesModels;


    public $user;

    /**
     * Create a new event instance.
     *
     * @param User $user
     */
    public function __construct(User $user)
    {
        //1 Add user instance . from Where?
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

添加事件处理到 User Model

    //Added events for User model
    protected $dispatchesEvents = [
        'creating' => UserCreate::class,
    ];

这样发生的事件就会到响应的类里,可以看到类里有广播.可有自定义的 BD 通道.

消费者订阅事件

光有事件还不行,哪里事件呢? 我倒是佩服这种脑洞大的用法…你倒是 1 条命令就全部搞完呀…

php artisan make:listener UserEventSubscirber

生成app/Listners/UserEventSubscirber

添加 Event handler:

public function onUserCreate($event)
{
    Log::info('Deleted event handle');
}
public  function onUserDeleted($event)
{

    Log::info('Deleted event handle');
}
//绑定handle与eventclass
public function subscribe($events)
{
    $events->listen(
        UserCreate::class,
        UserEventSubscirber::class . '@onUserDeleting'
    );

    $events->listen(
        UserUpdate::class,
        UserEventSubscirber::class . '@onUserDeleted'
    );
}

还需使订阅者生效:

// app/Providers/EventServiceProvider.php

protected $subscribe = [
    UserEventSubscriber::class
];

触发事件

最后是生产者了,就是在逻辑里使用该 model 即可.

$user->create();
$user->update();

观察者方式处理

上面的流程了解下Event,Listener是怎么工作的还好,但是太麻烦了..

迅猛的方法,直接创建 Observer,回调再里面写,so easy.

 php artisan make:observer UserObserver --model=User

有一丝丝想念 wordpress 的add_filter了…那就 1 个又快又好.