当前位置:网站首页>Thinkphp5 behavior hook return result (data) example
Thinkphp5 behavior hook return result (data) example
2022-07-28 11:27:00 【Curtain dust maple】
Reading guide :
In some scenes , We use behavior hooks Hook Do something , The result should be returned after execution ( data ), The returned value may be a string 、 Numbers or arrays . Don't know how to use behavior hook , First look at my previous article :
Thinkphp5.0 Action Hook Hook Usage of _ Mu CHENFENG's blog -CSDN Blog _thinkphp hook
step :
1 、 stay application/common.php Add the following methods to the public function file :
// Monitor a hook
function hook($hook,$params=array()){
// hinder true Parameter means to get only one valid return value , That is, only get the first element of the array
return \Think\Hook::listen($hook,$params,null,true);
}2、 stay application/tags.php Define behavior hooks
// Application behavior extension definition file
return [
// Application initialization
'app_init' => [],
// App start
'app_begin' => [],
// Module initialization
'module_init' => [],
// The operation begins
'action_begin' => [],
// View content filtering
'view_filter' => [],
// Log write
'log_write' => [],
// End of application
'app_end' => [],
// +----------------------------------------------------------------------
// | Custom behavior hook
// +----------------------------------------------------------------------
'sendData' => ['app\behavior\Order'],
];3、 Add a behavior file application/behavior/Order.php, The sample code is as follows :
<?php
namespace app\behavior;
use think\Db;
/**
* If there is only one behavior in this behavior class , Just define a behavior entry method `run` that will do
*/
class Order
{
// When the behavior class is bound to the hook ,run The behavior method will be executed when the hook is triggered
public function run($params){
// Behavioral logic
}
// A custom behavior method
public function sendData($params){
try{
// All business logic is omitted here
$data=array(
"code" => 1,
"msg" => ' Data sent successfully '
);
} catch (\Exception $e) {
$data=array(
"code" => 0,
"msg" => ' Sending data exception ,Error:'.$e->getMessage()
);
}
return $data;
}
}Look at the code to see ,Order.php Inside sendData() Method returns an array
4、 Call behavior method
// Use behavior hooks to send data
$params=array(
"account"=> "qq1234",
"money" => 10,
);
// It returns an array
$arr= hook('sendData',$params);
边栏推荐
- Stacks and queues
- The use of C language linked list
- A solution to the problem that ThinkPad fingerprint verification cannot be used in win7
- 2022-2023 年十大应用程序发展趋势
- Leetcode:1300. the sum of the array closest to the target value after transforming the array [dichotomy]
- 【一知半解】零值拷贝
- Inventory: 144 free learning websites, the most complete collection of resources in the whole network
- "Node learning notes" koa framework learning
- RHEL 6.4 installing SVN and Apache
- Fhwy workday schedule
猜你喜欢

开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开

不用Swagger,那我用啥?

Office2013 input mathematical formula above

BC35 NB模块AT指令开发总结

CVPR2021 行人重识别/Person Re-identification 论文+开源代码汇总

Learn these analysis methods and models, and no longer have no ideas when encountering problems

Inventory: 144 free learning websites, the most complete collection of resources in the whole network
![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]

融云 IM & RTC 能力上新盘点

苹果手机iCloud钥匙串的加密缺陷
随机推荐
Inventory: 144 free learning websites, the most complete collection of resources in the whole network
postgres概述
[MySQL] query multiple IDs and return string splicing
【MySQL从入门到精通】【高级篇】(九)InnoDB的B+树索引的注意事项
机器学习强基计划0-5:为什么学习的本质是泛化能力?
学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
抖音程序员表白专用代码教程(如何玩抖音)
Question of hanging the interviewer
【cesium】entity属性和时许绑定:SampledProperty方法简单使用
Purchase, sale and inventory software suitable for small and medium-sized enterprises to solve five major problems
Do you want to enroll in class for advanced soft exam
C语言使用二重指针实现简单工厂模式(多态)
PKG packaging node project
【C语言】的%*d、%.*s等详解:「建议收藏」
c语言实现float型数据转成BCD数据
win10安装sqlmap(windows 7)
Three ways for Cortex-M kernel to manage global interrupts
Ten thousand words detailed Google play online application standard package format AAB
精品方案|海泰方圆全栈式数据安全治理方案 为数据设一把“安全锁”
mysql的左连接和右连接(内连接和自然连接的区别)