当前位置:网站首页>Thinkphp5行为钩子Hook返回结果(数据)示例
Thinkphp5行为钩子Hook返回结果(数据)示例
2022-07-28 10:53:00 【幕尘枫】
导读:
在一些场景里,我们使用行为钩子Hook执行一些操作,执行完后要返回结果(数据),返回的值可能是字符串、数字或者数组。不懂用行为钩子的,先看我之前的这篇文章:
Thinkphp5.0行为钩子Hook的用法_幕尘枫的博客-CSDN博客_thinkphp 钩子
步骤:
1 、在application/common.php公共函数文件里添加如下方法:
//监听一个钩子
function hook($hook,$params=array()){
//后面的true参数表示只获取一个有效返回值,即只获取数组的第一个元素
return \Think\Hook::listen($hook,$params,null,true);
}2、在application/tags.php中定义行为钩子
// 应用行为扩展定义文件
return [
// 应用初始化
'app_init' => [],
// 应用开始
'app_begin' => [],
// 模块初始化
'module_init' => [],
// 操作开始执行
'action_begin' => [],
// 视图内容过滤
'view_filter' => [],
// 日志写入
'log_write' => [],
// 应用结束
'app_end' => [],
// +----------------------------------------------------------------------
// | 自定义的行为钩子
// +----------------------------------------------------------------------
'sendData' => ['app\behavior\Order'],
];3、添加一个行为文件 application/behavior/Order.php,示例代码如下:
<?php
namespace app\behavior;
use think\Db;
/**
* 若是该行为类中只有一个行为,只需要定义一个行为入口方法`run`即可
*/
class Order
{
// 当该行为类被绑定到钩子的时候,run行为方法则会在该钩子被触发的时候执行
public function run($params){
// 行为逻辑
}
//自定义的一个行为方法
public function sendData($params){
try{
//这里业务逻辑全部省略
$data=array(
"code" => 1,
"msg" => '发送数据成功'
);
} catch (\Exception $e) {
$data=array(
"code" => 0,
"msg" => '发送数据异常,Error:'.$e->getMessage()
);
}
return $data;
}
}看代码可以知道,Order.php里的sendData()方法返回的是一个数组
4、调用行为方法
//使用行为钩子发送数据
$params=array(
"account"=> "qq1234",
"money" => 10,
);
//返回的是一个数组
$arr= hook('sendData',$params);
边栏推荐
- Summary of the second semester of junior year
- Analysis of boot process of cortex-m4 and cortex-a7 kernel
- 小水滴2.0网站导航网模板
- Purchase, sale and inventory software suitable for small and medium-sized enterprises to solve five major problems
- 内存操作函数memcpy()和memmove()的用法
- Usage of memory operation functions memcpy() and memmove()
- What is WordPress
- 做数据分析,你还不懂RFM分析方法(模型)?
- Inventory: 144 free learning websites, the most complete collection of resources in the whole network
- Installation points and precautions of split angle probe
猜你喜欢

用手机对电脑进行远程关机

Technology sharing | quick intercom integrated dispatching system
![Leetcode:1300. the sum of the array closest to the target value after transforming the array [dichotomy]](/img/da/51c4051464d52eae10d93e881cfdf5.png)
Leetcode:1300. the sum of the array closest to the target value after transforming the array [dichotomy]

五面阿里技术专家岗,已拿offer,这些面试题你能答出多少
![Two point, three point, 01 point plan [bullet I]](/img/12/5cc55b5f4f0bbcd5b89a9601eed824.png)
Two point, three point, 01 point plan [bullet I]

Sword finger offer 06. print linked list from end to end

leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】

苹果手机iCloud钥匙串的加密缺陷

Inventory: 6 books teach you the necessary skills for career promotion

本地化、低时延、绿色低碳:阿里云正式启用福州数据中心
随机推荐
mysql还有哪些自带的函数呢?别到处找了,看这个就够了。
Understand several concepts of Oracle
Two dimensional prefix and
[MySQL] MySQL error "error 2006 (HY000): MySQL server has gone away"
JSON preliminary understanding
Usage of memory operation functions memcpy() and memmove()
leetcode:1300. 转变数组后最接近目标值的数组和【二分】
Use the common union and pointer to test the size end
Software designers ask 20 questions before the exam, pay attention!!
Generation and use of Lib library files in keil and IAR
JS - 修改数组中对象的键名
融云 IM & RTC 能力上新盘点
【MySQL从入门到精通】【高级篇】(十)MyISAM的索引方案&&索引的优缺点
18张图,直观理解神经网络、流形和拓扑
Inventory: exciting data visualization chart
C language to convert float data into BCD data
A solution to the problem that ThinkPad fingerprint verification cannot be used in win7
Relevant knowledge points of hash table
Two point, three point, 01 point plan [bullet I]
「Node学习笔记」Koa框架学习