首页 > 编程语言 >ThinkPHP行为和钩子实战,AOP编程

ThinkPHP行为和钩子实战,AOP编程

时间:2023-02-19 10:37:31浏览次数:45  
标签:function index 钩子 app Test params AOP ThinkPHP public


Demo

<?php

namespace app\index\controller;
use \think\facade\Hook;
class Index
{
public function index()
{
$params = ['username' => 'liaosp'];
Hook::add('app_init', 'app\index\behavior\Test');
Hook::listen('app_init', $params);
}
}

在index 模块中,新建 \index\behavior\Test.php

<?php

namespace app\index\behavior;
use think\Exception;
use think\Request;

class Test
{
public function run(Request $request,$params)
{
echo "kk";
// 行为逻辑
}
public function appInit($params)
{

throw new \Exception('更新失败');

}

public function appEnd($params)
{
echo "结束";
}
}

官网:
​​​点击访问​

实战,自己写一个AOP
​​​github仓库​

访问 index 即可访问Test 中的 appinit 方法。
实现了解耦,AOP过程。


标签:function,index,钩子,app,Test,params,AOP,ThinkPHP,public
From: https://blog.51cto.com/u_14131118/6066480

相关文章