侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Laravel事件的创建运行步骤

Laravel  /  管理员 发布于 2年前   1170

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", "alex@gmail.com"));
   }

最后一步是运行队列

php artisan queue:work

效果:

[2020-06-17 08:18:57][171] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[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 alex@gmail.com
[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 alex@gmail.com
[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 alex@gmail.com
[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 alex@gmail.com
[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 alex@gmail.com
[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 alex@gmail.com
[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 alex@gmail.com
[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 alex@gmail.com
[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 alex@gmail.com
[2020-06-17 08:19:17][180] Processed:  App\Listeners\SendRegistrationEmail



  • 上一条:
    php文件操作之文件读写浅析
    下一条:
    PHP封装返回Ajax字符串和JSON数组的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel 10.13版本发布(0个评论)
    • 在laravel中介绍一个生成假数据的PHP库:FakerPHP(0个评论)
    • 在laravel框架中对环境配置文件的加载过程步骤浅析(0个评论)
    • Laravel 10.12版本发布(0个评论)
    • 在laravel中以curl的方式请求京东联盟api获取数据流程步骤(0个评论)
    • 近期文章
    • 在go语言中对Grpc工具buf.build使用流程步骤(0个评论)
    • 宝塔面板Nginx开启Brotli压缩流程步骤,提升网站加载速度(0个评论)
    • 在go语言中如何判断用户代理是否为移动设备的示例(0个评论)
    • 在go语言中实现一个函数来检查用户代理是否是机器人或爬虫的示例(0个评论)
    • Laravel 10.13版本发布(0个评论)
    • 在github创建task的同时创建分支流程步骤(0个评论)
    • 在go语言中以邮件标题中获取SPF和DMARC,来判断是否为垃圾邮件之垃圾邮件过滤器功能实现(0个评论)
    • 在go语言中使用attr字段标签提取XML属性数据示例(0个评论)
    • 在laravel中介绍一个生成假数据的PHP库:FakerPHP(0个评论)
    • 在laravel框架中对环境配置文件的加载过程步骤浅析(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客