💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### 新建钩子文件及方法 ``` <?php namespace app\index\behavior; use app\common\model\LogModel; class Test { public function run($params) { // 行为逻辑 return $this->addlog($params); } /* * 添加日志 */ public function addlog($params) { $db = new LogModel(); $db->uid = $params; $db->status = 1; return $db->save(); // dump($db->save()); } } ``` ### 调用钩子 ``` 方式1 // 绑定钩子 $hook::add('app_init',['\app\index\behavior\Test']); // 监听钩子 并传递参数 $hook::listen('app_init',$user->id); 方式2 // 执行钩子 传递参数 Hook::exec('app\\index\\behavior\\Test', $user->id); ``` ### 测试 控制器方法中使用 ``` public function index(Request $request) { $data = $request->param(); $user = new UserModel(); $user->name = mt_rand(100, 999); $user->password = mt_rand(10000, 99999); $user->email = mt_rand(10000, 99999); $user->phone = mt_rand(1000000000, 19999999999); $user->age = mt_rand(20, 100); $user->status = mt_rand(0, 1); $user->save($data); $hook = new Hook(); $hook::add('app_init',['\app\index\behavior\Test']); $hook::listen('app_init',$user->id); // $result = Hook::exec('app\\index\\behavior\\Test', $user->id); return 11111111111; } ``` ### 创建sql ``` CREATE TABLE `log` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `uid` int(11) NOT NULL, `create_time` int(11) NOT NULL, `update_time` int(11) NOT NULL, `status` tinyint(1) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; CREATE TABLE `user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL, `password` varchar(32) NOT NULL, `email` varchar(32) NOT NULL, `phone` varchar(32) NOT NULL, `age` tinyint(3) NOT NULL, `create_time` int(11) NOT NULL, `update_time` int(11) NOT NULL, `status` tinyint(1) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; ``` ### 浏览器访问该控制器