laravel 事件的使用案例
以下是我对事件使用的一些记录
创建事件
执行以下命令,执行完成后,会在 app\Events 下面出现一个 DeleteEvent.php 文件,事件就在次定义
php artisan make:event DeleteEvent
编写事件
#DeleteEvent.php
<?php namespace App\Events; use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class DeleteEvent extends Event
{
use SerializesModels; public function __construct()
{
//
} public function broadcastOn()
{
print 'delete event';
}
}
创建监听listener
执行以下命令,执行完成后,会在 app\Listeners 下面出现一个 DeleteEventListener.php 文件,是对事件 DeleteEvent的监听
php artisan make:listener --event=DeleteEvent DeleteEventListener
编写事件监听
#DeleteEventListener.php
<?php namespace App\Listeners; use App\Events\DeleteEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; class DeleteEventListener
{
public function __construct()
{
//
} public function handle(DeleteEvent $event)
{
//
$event->broadcastOn();
}
}
调用事件-在控制器使用
#EventController.php
<?php namespace App\Http\Controllers; use App\Events\DeleteEvent;
use App\Events\SomeEvent;
use Illuminate\Http\Request; use App\Http\Requests; class EventController extends Controller
{
//
public function index()
{
// event(new SomeEvent()); //框架默认调用broadcastOn() $event = new DeleteEvent(); //自定义
event($event->broadcastOn());
}
}
编写路由
#routes.php
Route::get('/event',['uses'=>'EventController@index']);
laravel 事件的使用案例的更多相关文章
- laravel 事件机制 实践总结
laravel 事件机制 实践总结 观察者模式 在EventServiceProvider的linsten数组里面加上事件和监听器,键名是事件,键值里面的数组是一个或者多个监听器, protected ...
- Laravel事件Event
适用场景:记录文章浏览量 php artisan make:event 事件名 示例: php artisan make:event MyEvent Laravel目录\app\Events已经生成M ...
- laravel 事件监听
事件监听器监听到事件发生后会执行一些操作,Laravel使用观察者模式来实现这种监听机制 操作顺序:1.注册事件和监听器 -> 2.定义事件类 -> 3.定义监听类 -> 4.触发事 ...
- javascript之BOM事件注册和案例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- laravel 事件广播
Laravel 5.1 之中新加入了事件广播的功能,作用是把服务器中触发的事件通过websocket服务通知客户端,也就是浏览器,客户端js根据接受到的事件,做出相应动作.本文会用简单的代码展示一个事 ...
- C#学习笔记(11)——深入事件,热水器案例
说明(2017-6-14 15:04:13): 1. 热水器案例,为了便于理解,采用了蹩脚但直观的英文命名,哼哼. heater类,加热,声明一个委托,定义一个委托事件: using System; ...
- 简单易懂的laravel事件,这个功能非常的有用(监听事件,订阅者模式)
先说一下在什么场景会使用这个事件功能. 事情大概是这样的,需求要在用户注册的时候发一些帮助邮件给用户(原本用户在注册之后已经有发别的邮件的了,短信,IM什么的) 原来这个注册的方法也就10多行代码.但 ...
- Laravel 事件侦听的几个方法 [Trait, Model boot(), Observer Class]
1 Trait 1.1 可以在 Trait 中定义一个静态的 bootFooBar() 方法,注:FooBar 是你的 Trait 名称 namespace App\Traits; use App\A ...
- JS基础入门篇(二十)—事件对象以及案例(二)
案例一.点击按钮,选中input中的全部内容 select()方法:选中全部. 点击按钮选中输入框中的内容!!!! <!DOCTYPE html> <html lang=" ...
随机推荐
- [solr] - suggestion
前文使用了SpellCheck做了个自动完成模拟(Solr SpellCheck),使用第一种SpellCheck方式做auto-complete,是基于动态代码方式建立内容,下面方式可通过读文件方式 ...
- [solr] - IKAnalyzer 分词加入
1.下载IK Analyzer中文分词器:http://ik-analyzer.googlecode.com/files/IK%20Analyzer%202012FF_hf1.zip 2.解压出zip ...
- 学习C++的第一天
# include<iostream> //头文件的使用using namespace std; //命名空间的使用int main(){ int a,b; cin>>a ...
- 让background-color 无效
{ background-color: transparent; // 让背景透明,相当于背景颜色无效 }
- Gocd持续部署利器
http://www.go.cd/documentation/user/current http://www.go.cd/images/home-image1.png Gocd 是ThoughtWor ...
- 32. Path Sum && Path Sum II
Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...
- 创建struct类型的数组
在autoit中,如何创建类似这样的数组呢?如下方式,数组的element只是存储的地址相邻,所以我们可以这样做 $tagMYSTRUCT = "int code; char msg[10] ...
- Python图像处理库:Pillow 初级教程
Python图像处理库:Pillow 初级教程 2014-09-14 翻译 http://pillow.readthedocs.org/en/latest/handbook/tutorial.html ...
- PCI Express(二) - Topology
原文出处:http://www.fpga4fun.com/PCI-Express2.html Point-to-point architecture At 2.5Gbps, the PCI Expre ...
- js加载顺序
在jsp页面中引js文件,页面按照js顺序加载,若js中存在相同的方法,应该会执行顺序在前的js中的方法.有时候遇到js方法不执行或者执行不对的情况,可能就是js的顺序问题,有时候会找很久找不到问题. ...