当前位置:网站首页>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);
边栏推荐
- [JS advanced] JS functions, overloads, anonymous functions, scopes and scope chains_ 03
- What is WordPress
- win10安装sqlmap(windows 7)
- C language to convert float data into BCD data
- 大三下学期总结
- 融云 IM & RTC 能力上新盘点
- Two point, three point, 01 point plan [bullet I]
- 【Gradle】This version of the JMH Gradle plugin requires Gradle 6+, you are using 6.6.
- Google Earth Engine——使用geetool批量下载单景影像以Landsat 8 反演后的NDSI结果
- Machine learning strong foundation plan 0-5: why is the essence of learning generalization ability?
猜你喜欢

What is WordPress
Microsoft security team found an Austrian company that used windows Zero Day vulnerability to sell spyware

b2子主题/博客b2child子主题/开源源码

什么样的知识付费系统功能,更有利于平台与讲师发展?

Inventory: 144 free learning websites, the most complete collection of resources in the whole network

Software designers ask 20 questions before the exam, pay attention!!

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

ZBrush 2022软件安装包下载及安装教程

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

Two dimensional prefix and
随机推荐
Why is low code (apaas) popular again recently?
go status.go 状态码定义
Summary of the second semester of junior year
Two point, three point, 01 point plan [bullet I]
Understand several concepts of Oracle
Learn these analysis methods and models, and no longer have no ideas when encountering problems
Learning notes tree array
STM32 drives st7701s chip (V ⅰ V0 mobile phone screen change price)
微软安全团队发现一家利用Windows零日漏洞销售间谍软件的奥地利公司
leetcode:1300. 转变数组后最接近目标值的数组和【二分】
栈和队列
Use the common union and pointer to test the size end
DHCP实验演示(Huawei交换机设备配置)
The solution of PHP sending mobile MAS SMS garbled code
C语言使用二重指针实现简单工厂模式(多态)
b2子主题/博客b2child子主题/开源源码
What's the secret of creating a popular short video?
Software designers ask 20 questions before the exam, pay attention!!
Flutter教程之带有 GoRouter 的 Flutter Navigator 2.0,使用 go_router 包了解 Flutter 中的声明式路由机制(教程含源码)
JWT login authentication + token automatic renewal scheme, well written!