annotation注解路由

2.0.1 版本
2024-03-21 版本更新时间
559 安装
19 star

webman annotation 注解

Security Status


使用了 doctrine/annotations 包来对代码内的注解进行解析。支持php8注解方式

您可以直接在控制器类任意方法定义@RequestMapping注解来完成一个路由的定义,如需使用路由中间件请定义该路由的注解方法@Middwares@Middware
注解并引入中间件命名空间即可

感谢各位大佬批评指正,不足之处,还望海涵!

安装

composer require sunsgne/annotations

使用

路由控制

  • GET
  • POST
  • PUT
  • DELETE
  • HEADER
  • OPTIONS
    use Sunsgne\Annotations\Mapping\RequestMapping;
    /**
    * 允许通过 GET 或 POST 方式请求
    * @RequestMapping(methods="GET , POST" , path="/api/json")
    * @param Request $request
    * @return Response
    */
    public function json(Request $request)
    {
    return json(['code' => 0, 'msg' => 'ok']);
    }

路由中间件

在通过注解定义路由时,您仅可通过注解的方式来定义中间件,对中间件的定义有两个注解,分别为:

使用 @Middleware 注解时需 use Sunsgne\Annotations\Mapping\Middleware; 命名空间;

使用 @Middlewares 注解时需 use Sunsgne\Annotations\Mapping\Middlewares; 命名空间;

  • @Middleware 注解为定义单个中间件时使用,在一个地方仅可定义一个该注解,不可重复定义
  • @Middlewares 注解为定义多个中间件时使用,在一个地方仅可定义一个该注解,然后通过在该注解内定义多个 @Middleware 注解实现多个中间件的定义

定义单个中间件:

use Sunsgne\Annotations\Mapping\RequestMapping;
use Sunsgne\Annotations\Mapping\Middleware;
use Sunsgne\Annotations\Mapping\Middlewares;
use app\middleware\App;
use app\middleware\Log;
/**
 * @RequestMapping(methods="GET" , path="/api/json")
 * @Middleware(App::class)
 * @param Request $request
 * @return Response
 */
public function json(Request $request)
{
    return json(['code' => 0, 'msg' => 'ok']);
}

定义多个中间件:

use Sunsgne\Annotations\Mapping\RequestMapping;
use Sunsgne\Annotations\Mapping\Middleware;
use Sunsgne\Annotations\Mapping\Middlewares;
use app\middleware\App;
use app\middleware\Log;
/**
 * @RequestMapping(methods="GET" , path="/api/json")
 * @Middlewares({
 *     @Middleware(App::class),
 *     @Middleware(Log::class)
 * })
 * @param Request $request
 * @return Response
 */
public function json(Request $request)
{
    return json(['code' => 0, 'msg' => 'ok']);
}

支持PHP8.0+版本


*注意请勿直接copy。示例中未创建中间件

  1. 定义路由

    use Sunsgne\Annotations\Mapping\RequestMapping;
    use Sunsgne\Annotations\Mapping\Middleware;
    use Sunsgne\Annotations\Mapping\Middlewares;
    use app\middleware\App;
    use app\middleware\Log;
    #[RequestMapping(methods: "GET , POST" , path:"/api/json")]
    public function json(Request $request)
    {
    return json(['code' => 0, 'msg' => 'ok']);
    }
  2. 定义路由并配置中间件(多个)

    use Sunsgne\Annotations\Mapping\RequestMapping;
    use Sunsgne\Annotations\Mapping\Middleware;
    use Sunsgne\Annotations\Mapping\Middlewares;
    use app\middleware\App;
    use app\middleware\Log;
    #[RequestMapping(methods: "GET , POST" , path:"/api/json") , Middlewares(App::class , Log::class)]
    public function json(Request $request)
    {
    return json(['code' => 0, 'msg' => 'ok']);
    }

忽略注解参数

请在config/plugin/sunsgne/annotations/ignored文件中添加需要忽略的参数

return [
    "after", "afterClass", "backupGlobals", "backupStaticAttributes", "before", "beforeClass", "codeCoverageIgnore*",
    "covers", "coversDefaultClass", "coversNothing", "dataProvider", "depends", "doesNotPerformAssertions",
    "expectedException", "expectedExceptionCode", "expectedExceptionMessage", "expectedExceptionMessageRegExp", "group",
    "large", "medium", "preserveGlobalState", "requires", "runTestsInSeparateProcesses", "runInSeparateProcess", "small",
    "test", "testdox", "testWith", "ticket", "uses" , "datetime" 
    // ........
];

更新日志

1.1.2 - 2022-07-04

  • 修复注解含有混杂参数,导致读取失败的问题
  • 新增配置文件ignored.php,用于对注解中的其他参数做忽略读取操作:如datetime,used等。
  • php8以上版本做原生注解的适配
赞助商