一、事件的监听与响应
1,基本介绍
(1)Eloquent 模型可以在模型生命周期中的各个时间点触发相应的事件:
- retrieved:从数据库中获取已存在模型时会触发该事件。
- creating、created:当一个新模型被首次保存的时候,这两个事件会被触发。
- updating、updated:当一个模型已经在数据库中存在并调用 save 方法,这两个事件会被触发。
- saving、saved:无论是创建还是更新,这两个事件都会被触发。
(2)我们可以通过事件实现:在一个指定模型类每次保存或更新的时候执行相应的代码。
2,使用样例
(1)首先我们打开 app/Providers/EventServiceProvider.php 文件,注册一个事件监听器映射关系:
<?php namespace App\Providers; use Illuminate\Support\Facades\Event; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ Registered::class => [ SendEmailVerificationNotification::class, ], 'App\Events\UserSaved' => [ 'App\Listeners\UserSavedListener', ], ]; /** * Register any events for your application. * * @return void */ public function boot() { parent::boot(); // } }
(2)接着在终端中进入项目文件夹,然后执行如下 Artisan 命令:
php artisan event:generate
(3)上面命令执行后,会分别自动在 app/Events 和 app/Listensers 目录下生成对应的事件类和监听类。
(4)修改生成的 UserSaved.php 文件内容:
<?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\Models\User; class UserSaved { use Dispatchable, InteractsWithSockets, SerializesModels; public $user; /** * Create a new event instance. * * @return void */ public function __construct(User $user) { $this->user = $user; } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn() { return new PrivateChannel('channel-name'); } }
(5)修改生成的 UserSavedListener.php 文件内容:
<?php namespace App\Listeners; use App\Events\UserSaved; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; class UserSavedListener { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param UserSaved $event * @return void */ public function handle(UserSaved $event) { echo "--- saved事件响应,保存对象如下 ----\n"; $user = $event->user; echo(json_encode($user)); } }
(6)修改模型 User.php,在模型保存时发出 UserSaved 这个自定义事件。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; use App\Events\UserSaved; class User extends Model { use Notifiable; /** * The event map for the model. * * @var array */ protected $dispatchesEvents = [ 'saved' => UserSaved::class, //'deleted' => UserDeleted::class, ]; public $timestamps = false; }
(7)下面进行测试,我们获取一个用户,修改其属性后保存。
$user = User::find(1); $User->age = 44; $user->save();
(8)运行结果如下:可以看到事件监听响应成功。
二、事件的订阅
1,基本介绍
(1)事件订阅(Event Subscribers)是一种特殊的 Listener,前面讲的是一个 listener 里只能放一个 hander()。
(2)事件订阅可以把很多处理器(handler)放到一个类里面,然后用一个 listner 把它们集合起来,这样不同的事件只要对应一个 listner 就可以了。
2,使用样例
(1)假设我们 User 模型这边在保存和删除时会分别发出 UserSaved、UserDeleted 这两个自定义事件(这两个自定义事件内容我就不写了,具体可参考上面的文章内容)。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; use App\Events\UserSaved; class User extends Model { use Notifiable; /** * The event map for the model. * * @var array */ protected $dispatchesEvents = [ 'saved' => UserSaved::class, 'deleted' => UserDeleted::class, ]; public $timestamps = false; }
(2)接着我们定义一个同时处理这两个事件的 Listener:
<?php namespace App\Listeners; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; class UserEventListener { // 用户保存事件响应 public function onUserSaved($event) { echo "--- onUserSaved ----\n"; $user = $event->user; echo(json_encode($user)); } // 用户删除事件响应 public function onUserDeleted($event) { echo "--- onUserDeleted ----\n"; $user = $event->user; echo(json_encode($user)); } // 同时订阅多个事件 public function subscribe($events) { $events->listen( 'App\Events\UserSaved', 'App\Listeners\UserEventListener@onUserSaved' ); $events->listen( 'App\Events\UserDeleted', 'App\Listeners\UserEventListener@onUserDeleted' ); } }
(3)最后在 EventServiceProvider.php 注册下就可以了:
<?php namespace App\Providers; use Illuminate\Support\Facades\Event; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ Registered::class => [ SendEmailVerificationNotification::class, ] ]; // 注册订阅 protected $subscribe = [ 'App\Listeners\UserEventListener', ]; /** * Register any events for your application. * * @return void */ public function boot() { parent::boot(); // } }
三、观察者(Observers)
1,基本介绍
如果需要监听模型中的多个事件,我们还可以使用观察者。观察者类可以反射我们想要监听的 Eloquent 事件对应的方法名,并且每个方法接收模型作为唯一参数。
2,使用样例
(1)使用观察者的话,模型这边就不需要再指定发出哪些事件了:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { }
(2)接着我们创建一个User模型对应的观察者类(UserObserver)。观察者类的方法即为事件名,这样事件就可以通过反射自动找到对应的方法。
<?php namespace App\Observers; use App\Models\User; class UserObserver { /** * 监听用户创建事件. * * @param User $user * @return void */ public function saved(User $user) { echo "--- onUserSaved ----\n"; echo(json_encode($user)); } /** * 监听用户删除事件. * * @param User $user * @return void */ public function deleted(User $user) { echo "--- onUserDeleted ----\n"; echo(json_encode($user)); } }
(3)最后在 app/Providers/AppServiceProvider.php 中注册观察者即可:
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Models\User; use App\Observers\UserObserver; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { // } /** * Bootstrap any application services. * * @return void */ public function boot() { User::observe(UserObserver::class); } }
Laravel - MySQL数据库的使用详解系列: