首页 > 代码库 > Laravel之事件

Laravel之事件

一.事件

事件无处不在,比如用户登录、购买商品、搜索、查看文章,等等,都是事件,有了事件,就有事件监听器,事件监听器监听到事件发生后会执行一些操作,Laravel使用观察者模式来实现这种监听机制。本节我们通过一个简单的示例来讲述在Laravel中如何创建事件类、事件对应的监听器类,以及监听器如何监听事件发生并执行相应操作。

这里我们实现一个添加任务后后写入日志的事件

 

二.注册事件-监听器

1.我们需要在EventServiceProvider中注册事件与监听器之间的映射关系:

protected $listen = [
‘App\Events\TaskAdded‘=>[
‘App\Listeners\SaveTaskToLogListener‘
]
];

  

2.生成事件

php artisan event:generate

  

该命令会在app/Events目录下生成TaskAdded.php,在app/Listeners目录下生成SaveTaskToLogListener.php。

 

三.定义事件类TaskAdded.class

<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Models\Task;

class TaskAdded extends Event
{
    use SerializesModels;
    public $task;

    /**
     * TaskAdded constructor.
     * 构造函数,注入一个Task实例
     * @param Task $task
     */
    public function __construct(Task $task)
    {
        $this->task = $task;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}

  

事件类什么都没做,只是注入了一个Task实例

 

四.事件监听器类SaveTaskToLogListener.php

<?php

namespace App\Listeners;

use Illuminate\Support\Facades\Log;
use App\Events\TaskAdded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    /**
     * Handle the event.
     *
     * @param  TaskAdded  $event
     * @return void
     */
    public function handle(TaskAdded $event)
    {
        $task = $event->task;
        Log::alert(‘添加了一个任务‘, [‘id‘ => $task->id, ‘name‘ => $task->name]);
    }
}

  

五.触发事件

$task = Task::create([‘user_id‘=>Auth::id(), ‘name‘ => $request->input(‘name‘)]);
// 触发事件
Event::fire(new TaskAdded($task));

  

六.事件订阅者

事件订阅者是指那些在类本身中订阅到多个事件的类,从而允许你在单个类中定义一些事件处理器。订阅者应该定义一个subscribe 方法,该方法中传入一个事件分发器实例;简言之就是一个事件监听器类,处理了事件;一个监听器监类依靠不同的方法监听了多个事件类

 

1.监听器类代码(subscribe中设置了监听)

 

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

    public function onAddTask(TaskAdded $event)
    {
        $task = $event->task;
        Log::info(‘增加了task‘, [‘id‘ => $task->id, ‘name‘ => $task->name]);
    }

    public function onDelTask(TaskDeled $event)
    {
        $task = $event->task;
        Log::info(‘删除了task‘, [‘id‘ => $task->id, ‘name‘ => $task->name]);
    }

    public function subscribe($events)
    {
        $events->listen(‘App\Events\TaskAdded‘,‘App\Listeners\TaskToLogListener@onAddTask‘);

        $events->listen(‘App\Events\TaskDeled‘,‘App\Listeners\TaskToLogListener@onDelTask‘);
    }
}

  

2.事件类

TaskAdded.class:

class TaskAdded extends Event
{
    use SerializesModels;
    public $task;

    /**
     * TaskAdded constructor.
     * 构造函数,注入一个Task实例
     * @param Task $task
     */
    public function __construct(Task $task)
    {
        $this->task = $task;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}

  

TaskDeled.class:

class TaskDeled extends Event
{
    use SerializesModels;
    public $task;

    /**
     * TaskAdded constructor.
     * 构造函数,注入一个Task实例
     * @param Task $task
     */
    public function __construct(Task $task)
    {
        $this->task = $task;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}

  

3.在EventServiceProvider添加属性$subscribe

protected $subscribe = [TaskToLogListener::class];

  

4.触发事件

Event::fire(new TaskAdded($task));
Event::fire(new TaskDeled($task));

  

 

Laravel之事件