首页 > 编程语言 >Thinkphp8 通过中间件 和 属性 简单实现AOP

Thinkphp8 通过中间件 和 属性 简单实现AOP

时间:2024-08-13 16:49:56浏览次数:9  
标签:function Thinkphp8 use app 中间件 response handler AOP public

转载请著名来源,侵权必究

//属性接口
declare(strict_types=1);

namespace app\common\attribute;


interface AttributeHandler
{
    public function handler(mixed $handler);
}

//不需要登陆属性
declare(strict_types=1);

namespace app\common\attribute;


/**
 * 跳过登陆验证
 */
#[\Attribute(\Attribute::TARGET_METHOD)]
class NoLogin implements AttributeHandler
{
    public function __construct(){

    }

    public function handler(mixed $handler)
    {
        // TODO: Implement handler() method.
        //return Response::create('页面未找到 - NoLogin', 'html', 404);
    }
}


//权限验证属性
declare(strict_types=1);

namespace app\shop\attribute;


use app\common\attribute\AttributeHandler;

/**
 * 权限验证
 */
#[\Attribute(\Attribute::TARGET_METHOD)]
class CheckPermission implements AttributeHandler
{
    public function __construct(private string $permission){
    }

    public function handler(mixed $handler)
    {
        // TODO: Implement handler() method.
        //return Response::create('页面未找到 - CheckPermission', 'html', 404);
    }
}

注意这里是要加到 控制器中间件

//属性中间件 (控制器中间件)
declare(strict_types=1);

namespace app\common\middleware;


use app\common\attribute\AttributeHandler;
use Closure;
use think\App;
use think\Request;
use think\Response;
use think\Session;

/**
 * 属性中间件
 */
class AttributeMiddleware
{
    public function __construct(protected App $app, protected Session $session)
    {
    }

    public function handle(Request $request, Closure $next): Response
    {
        /** @var Response $response */
        $response = null;
        $reflect = new \ReflectionClass('app\\'.app('http')->getName().'\controller\\'.$request->controller());
        $attributes = $reflect->getMethod($request->action())->getAttributes();
        foreach($attributes as $attribute){
            /**
             * @var AttributeHandler $instance
             * **/
            $instance = $attribute->newInstance();
            $response = $instance->handler($this);
            if($response instanceof Response){
                return $response;
            }
        }
        $request->attributes = $attributes;
        $response = $next($request);
        return $response;
    }
}

//测试类
<?php

namespace app\shop\controller;

use app\common\attribute\NoLogin;
use app\shop\attribute\CheckPermission;
use app\shop\BaseController;

class Index extends BaseController
{
    #[CheckPermission(permission:"Foo")]
    public function index()
    {
        return '<style>*{ padding: 0; margin: 0; }</style><iframe src="https://www.thinkphp.cn/welcome?version=' . \think\facade\App::version() . '" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe>';
    }

    #[NoLogin]
    public function hello($name = 'ThinkPHP8')
    {
        return 'hello,' . $name;
    }
}

标签:function,Thinkphp8,use,app,中间件,response,handler,AOP,public
From: https://www.cnblogs.com/kuangke/p/18357295

相关文章

  • DBSyncer:一款开源的数据同步中间件
    dbmotiondbsyncerdatax…介绍DBSyncer(英[dbsɪŋkɜː®],美[dbsɪŋkɜː®简称dbs)是一款开源的数据同步中间件,提供MySQL、Oracle、SqlServer、PostgreSQL、Elasticsearch(ES)、Kafka、File、SQL等同步场景。支持上传插件自定义同步转换业务,提供监控全量和增量数据统计......
  • 过滤器、拦截器、AOP、ControllerAdvcie执行顺序对比
    过滤器、拦截器、AOP、ControllerAdvcie执行顺序对比0.执行顺序过滤器➡拦截器➡AOP➡ControllerAdvice➡Controller没有异常的情况下,执行顺序如下:有异常的情况下,执行顺序如下:tip:当产生异常后,无论是否有ControllerAdvice处理,HandlerInterceptor都不会执行post......
  • Spring源码系列六:AOP
    更多请关注:https://t.zsxq.com/fhroW文章目录ProxyFactory使用ProxyFactoryAdviceThrowsAdviceMethodInterceptorAdvisor通过spring产生代理对象TargetSourceSpringAop创建流程判断ProxyFactory使用JDK代理还是CGLIB代理拓展ProxyFactoryProxyFactory是Sprin......
  • AOP -面向切面编程
    AOP-面向切面编程aop是oop(面向对象编程)的补充和完善。oop是一种纵向开发,然而当面对需要一些横向的功能如日志,就会导致大量重复的代码。aop利用横切把一些重复的非核心的代码插入到核心代码周围,不需要重复写很多遍。应用场景:日志记录,在方法的执行前后插入日志功能事务处......
  • Java设计模式和AOP编程
    Java六大设计原则;Java23种设计模式(在此介绍三种设计模式)Java设计模式单例模式应用场景:spring中bean的作用域用的就是单例模式//基本的单例模式————懒汉式publicclassstudent{//3.创建static修饰的成员变量privatestaticstudentstu;//1.设计私......
  • AOP切面编程
    AOP切面编程一些知识切面:处理共同逻辑的模块@Aspect 用在类上,表示这个类是一个切面目标:被切面作用的业务模块切入点:用于指定那些切面作用于哪些目标组件上,一般用表达式实现。通知:切面和切入点的执行循序;分为:前置通知,后置通知,最终通知,环绕通知,异常通知前置通知(BeforeAdvic......
  • 常见的中间件漏洞:WebLogic
     WebLogic        WebLogic是美国Oracle公司出品的⼀个applicationserver,确切的说是⼀个基于JAVAEE架构的中间件,默认端⼝:7001WebLogic是⽤于开发、集成、部署和管理⼤型分布式Web应⽤、⽹络应⽤和数据库应⽤的Java应⽤服务器。将Java的动态功能和JavaEnterprise......
  • 常见的中间件漏洞:Tomcat
    Tomcat简介        tomcat是一个开源而且免费的isp服务器,默认端口:8080,属于轻量级应用服务器。它可以实现JavaWeb程序的装载,是配置JSP(JavaServerPage)和JAVA系统必备的一款环境。在历史上也披露出来了很多的漏洞,这里我们讲几个经典的漏洞复现一.CVE-2017-12615To......
  • Spring AOP:面向切面编程的最佳实践 ( 一 )
    1.AOP思想1.1.为什么需要面向切面编程如果在一个类或者多个类的多个业务逻辑方法中,在开始,结尾部分包含功能相同的代码称之为横切关注点也叫切面,这种结构可能符合传统的面向对象编程(OOP)的封装特性,但可能导致代码难以维护和扩展。面向切面编程是一种编程范式。它允......
  • laravel: 用中间件把请求和响应log下来
    一,代码:1,中间件<?phpnamespaceApp\Http\Middleware;useClosure;useIlluminate\Http\Request;useSymfony\Component\HttpFoundation\Response;useApp\extend\LogEs;classLogSearch{/***Handleanincomingrequest.**@par......