laravel事件監(jiān)聽--模型事件新特性

<?php
namespace App;
use App\Events\PostWasPublished;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $events = [
        'created' => PostWasPublished::class  , //key就是事件的名字,值就是觸發(fā)的事件。這個(gè)事件可以是一個(gè)完整的類
    ];
}

當(dāng)執(zhí)行到這個(gè)model里面created方法的時(shí)候,會(huì)觸發(fā)PostWasPublished事件。這個(gè)事件也就是PostWasPublished類里面的代碼。它背后的監(jiān)聽著,其實(shí)就是PostWasPublishedListenr類里面的代碼

然后我們?nèi)?code>EventServiceProvider類里面修改$listen屬性

protected $listen = [
        'App\Events\PostWasPublished' => [
            'App\Listeners\PostWasPublishedListenr',
// 如果一個(gè)事件需要發(fā)送給多個(gè)監(jiān)聽者,我們只需要把監(jiān)聽的類寫入這個(gè)數(shù)組里面
        ],
    ];

此時(shí)執(zhí)行php artisan event:generate會(huì)生成兩個(gè)類
Events文件中生成PostWasPublished類,Listeners文件中生成PostWasPublishedListenr類,也就是上面$listen屬性定義的兩個(gè)類。

生成兩個(gè)類

PostWasPublished發(fā)布類

<?php

namespace App\Events;
use App\Post;
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;

class PostWasPublished
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $post;   // 注意此處要被繼承使用,必須用publish
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Post $post)
    {
        $this->post = $post;
    }

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

PostWasPublishedListenr監(jiān)聽類

<?php

namespace App\Listeners;

use App\Events\PostWasPublished;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class PostWasPublishedListenr
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  PostWasPublished  $event
     * @return void
     */
    public function handle(PostWasPublished $event)
    {

// 因?yàn)楸O(jiān)聽類PostWasPublished里面注入了Pos這個(gè)model類,所以我們可以直接使用Post里面的函數(shù)
        dump($event->post->toArray());
    }
}

路由

注意此處用的是 create  而不是created
Route::get('/', function () {
   return \App\User::create(['name'=>'name','email'=>'62em11ail3@qq.com','password'=>'password']);
});


也可以直接在路由里使用event全局函數(shù)做觸發(fā)
以下方式跟User的模型里面 $events 屬性就沒有關(guān)系了,這是直接觸發(fā)的
這種打方式會(huì)觸發(fā)兩次事件
Route::get('/', function () {
    $user = \App\User::create(['name'=>'name','email'=>'652em11ail3@qq.com','password'=>'password']);
    event(new \App\Events\PostPublished($user));
});

這種方式會(huì)觸發(fā)一次事件
Route::get('/', function () {
    $user = \App\User::find(2);
    event(new \App\Events\PostPublished($user));
});
圖片.png
  • 但是可以監(jiān)聽的事件只有以下函數(shù)中的那些
圖片.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容