前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Laravel Event和Listener

Laravel Event和Listener

作者头像
苦咖啡
发布2018-04-28 13:27:30
1.2K0
发布2018-04-28 13:27:30
举报
文章被收录于专栏:我的博客我的博客

make:event TestEvent 创建一个新事件类 php artisan make:listener TestListener –event TestEvent 创建一个listener类 注册事件TestEvent /** * Create a new event instance. * * @return void */ public function __construct($str) { $this->data = $str; }

/** * Get the channels the event should broadcast on. * * @return Channel|array */ public function broadcastOn() { return []; } 监听时间TestListener /** * Create the event listener. * * @return void */ public function __construct() { // }

/** * Handle the event. * * @param TestEvent $event * @return void */ public function handle(TestEvent $event) { file_put_contents(‘/data/tmp/test.log’, $event->data, FILE_APPEND); } 注册应用关联事件EventServiceProvider.php protected $listen = [

‘app\Events\TestEvent’ => [ ‘app\Listeners\TestListener’, ], ]; 触发事件 $str = ‘test….’; Event::fire(new TestEvent($str)); event(new TestEvent($str));

php artisan event:generate 批量生成event和listen 编辑EventServiceProvider.php

备注:事件可以广播,通过websocket服务,依赖Redis的pub和sub模式

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年3月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档