laravel事件监听器
2024-09-05 06:11:59
在EventServiceProvide文件里注册事件和监听
protected $listen = [
'App\Events\SendPhoneCodeEvent' => [
'App\Listeners\SendPhoneCodeListener',
]
];
1.建立事件文件Events/SendPhoneCodeEvent.php
<?php
namespace App\Events; class SendPhoneCodeEvent
{
use SerializesModels; public $data; /**
* 创建一个事件实例。
*
* @param \App\Order $order
* @return void
*/
public function __construct($data)
{
$this->data = $data;
}
}
2.建立监听文件 Listeners/SendPhoneCodeListener.php
<?php
namespace App\Listeners; use App\Events\SendPhoneCodeEvent;
use Illuminate\Support\Facades\Log; class SendPhoneCodeListener
{
/**
* 创建事件监听器。
*
* @return void
*/
public function __construct()
{ } /**
* 处理事件。
*
* @param \App\Events\SendPhoneCodeEvent $event
* @return void
*/
public function handle(SendPhoneCodeEvent $event)
{ Log::info("Listener:".$event->data);
} public function failed()
{
}
}
4.有控制器里调用事件
use App\Events\SendPhoneCodeEvent; event(new SendPhoneCodeEvent(''));
最新文章
- 30分钟让网站支持HTTPS
- 求助sublime snippet
- 虚析构函数(√)、纯虚析构函数(√)、虚构造函数(X)
- 在EF的code frist下写稳健的权限管理系统:仓储设计(三)
- 用MSBuild和Jenkins搭建持续集成环境(2)
- web.xml中servlet初始化参数的设置
- HeadFirst设计模式之代理模式
- poj 1696 Space Ant(模拟+叉积)
- 一个简单的shell脚本
- Error with mysqld_safe
- MySQL TIMESTAMP(时间戳)详解
- JSP page指令
- 7种方法解决移动端Retina屏幕1px边框问题
- java枚举类(enum) 基础知识讲解
- Django之路由分发系统
- 不错的东西: AutoMapper
- c# 获取 com 引用真实组件地址
- 3,postman的变量写法和collection
- Yii的操作提示框
- 近几年ACM/ICPC区域赛铜牌题