Laravel事件的创建运行步骤

管理员 发布于 4年前   474

Laravel事件的创建运行步骤


看看官网的介绍:

Eloquent 模型触发几个事件,允许你挂接到模型生命周期的如下节点: retrieved、 creating、 created、 updating、 updated、 saving、 saved、 deleting、 deleted、 restoring 和 restored。事件允许你每当特定模型保存或更新数据库时执行代码。每个事件通过其构造器接受模型实例。


retrieved 事件在现有模型从数据库中查找数据时触发。当新模型首次被保存时, creating 和 created 事件被触发。如果数据库中已经存在模型并且调用了 save 方法, updating / updated 事件被触发。这些情况下, saving / saved 事件也被触发。


事件提供了一种简单的观察器实现,该实现允许用户订阅和收听Web应用程序中触发的各种事件。 Laravel中的所有事件类都存储在app / Events文件夹中,而侦听器则存储在app / Listeners文件夹中。


创建

php artisan event:generate
php artisan make:event UserRegisteredEvent
php artisan make:listener SendMailListener --event="UserRegisteredEvent"

UserRegisteredEvent.php

<?php
namespace App\Events;

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

class UserRegisteredEvent
{
   use Dispatchable, InteractsWithSockets, SerializesModels;

   public $title;
   public $body;
   public $to;

   public function __construct($title, $body, $to)
   {
       $this->title = $title;
       $this->body = $body;
       $this->to = $to;
   }

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

<?php
namespace App\Listeners;

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

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

   /**
    * Handle the event.
    *
    * @param  UserRegisteredEvent  $event
    * @return void
    */
   public function handle(UserRegisteredEvent $event)
   {
       echo "Start sending email".PHP_EOL;
       sleep(2);
       echo "Email sended to {$event->to}".PHP_EOL;
   }
}

EventServiceProvider.php

<?php
namespace App\Providers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
   /**
    * The event listener mappings for the application.
    *
    * @var array
    */
   protected $listen = [
       'App\Events\UserRegistered' => [
           'App\Listeners\SendRegistrationEmail',
       ],
   ];

   /**
    * Register any events for your application.
    *
    * @return void
    */
   public function boot()
   {
       parent::boot();

       //
   }
}

运行事件 (例如:在您的控制器中添加类似的内容)

    for ($i=0; $i < 10; $i++) {
           event(new UserRegisteredEvent("hi", "how are you", "[email protected]"));
   }

最后一步是运行队列

php artisan queue:work

效果:

[2020-06-17 08:18:57][171] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[2020-06-17 08:18:59][171] Processed:  App\Listeners\SendRegistrationEmail
[2020-06-17 08:18:59][172] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[2020-06-17 08:19:01][172] Processed:  App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:01][173] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[2020-06-17 08:19:03][173] Processed:  App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:03][174] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[2020-06-17 08:19:05][174] Processed:  App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:05][175] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[2020-06-17 08:19:07][175] Processed:  App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:07][176] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[2020-06-17 08:19:09][176] Processed:  App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:09][177] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[2020-06-17 08:19:11][177] Processed:  App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:11][178] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[2020-06-17 08:19:13][178] Processed:  App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:13][179] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[2020-06-17 08:19:15][179] Processed:  App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:15][180] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[2020-06-17 08:19:17][180] Processed:  App\Listeners\SendRegistrationEmail


请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成

是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客

      订阅博客周刊

文章标签

友情链接

HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群
侯体宗的博客