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

版权声明:廖圣平博客,未经博主允许不得转载。企鹅:1194008361 https://blog.csdn.net/qq_22823581/article/details/86521551

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

猜你喜欢

转载自blog.csdn.net/qq_22823581/article/details/86521551