guanhui07/webman-annotation

Webman plugin sunsgne/annotations

dev-master 2024-09-19 09:57 UTC

This package is auto-updated.

Last update: 2024-11-19 10:17:51 UTC


README

sunsgne

sunsgne/annotation

🐬 Webman's shortcut routing based on annotation scheme 🐬

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

webman annotation 注解路由

使用了 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']);
}
  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") , 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以上版本做原生注解的适配