0
点赞
收藏
分享

微信扫一扫

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


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过程。


举报

相关推荐

0 条评论