webman 框架 注解路由插件
Github 地址:https://github.com/shayvmo/webman-annotations
webman 框架 注解路由插件
使用了 doctrine/annotations 包来对代码内的注解进行解析。
您可以直接在控制器类或类方法定义注解,实现路由定义。
webman框架插件地址:https://www.workerman.net/plugin/115
站在巨人的肩膀可以看到更远,感谢 https://www.workerman.net/plugin/52 的启发。
更新日志
v1.1.1
2023-04-11,修复发现的RequestMapping解析allow_methods问题
v1.1.0
2023-03-30,增加php8原生注解支持
v1.0.1
2023-03-27,修复发现的bug
v1.0.0
2023-03-27,发布1.0.0版本
安装
composer require shayvmo/webman-annotations |
使用
配置文件
|
一、中间件注解
注:方法会继承类定义的中间件。
类和方法通用,参数中间件类名,单个中间件传入字符串,多个中间件传入字符串数组。
use Shayvmo\WebmanAnnotations\Annotations\Middleware; |
二、类注解
类注解有控制器注解@RestController和资源路由@ResourceMapping。
资源路由和webman框架原有的资源路由一致。参考:webman路由
控制器注解
use Shayvmo\WebmanAnnotations\Annotations\RestController; |
@RestController控制器注解,只有一个参数prefix,表示整个控制器的路由路径前缀,方法路由路径都会拼接该前缀。
传参可以省略键名。
@RestController("/a")@RestController(prefix="/a")
php8注解
#[RestController("/test1")]#[RestController(path: "/test2")]
资源路由注解
use Shayvmo\WebmanAnnotations\Annotations\ResourceMapping; |
@ResourceMapping资源路由注解,有path 和 allow_methods两个参数path表示资源路由的路径,allow_methods为指定的资源方法数组,不传指定资源方法时,使用全部资源方法path传参可以省略键名。
@ResourceMapping(path="/dddd", allow_methods={"index", "show"})@ResourceMapping("/dddd", allow_methods={"index", "show"})
php8注解
#[ResourceMapping("/test", allow_methods: ["index", "show"])]#[ResourceMapping(path: "/test2", allow_methods: ["index", "show"])]
注:如果定义了资源路由,会自动忽略类同名方法的方法注解。
附:资源路由方法对照
| 请求方法 | 路径 | 类方法 |
|---|---|---|
| GET | /test | index |
| GET | /test/create | create |
| POST | /test | store |
| GET | /test/{id} | show |
| GET | /test/{id}/edit | edit |
| PUT | /test/{id} | update |
| DELETE | /test/{id} | destroy |
| PUT | /test/{id}/recovery | recovery |
三、方法注解
方法注解主要是@RequestMapping 以及 @GetMapping、@PostMapping、@PutMapping、@DeleteMapping 四个便捷注解。
定义路由路径 path 和请求方法methods。两个参数均可以传入字符串或数组。
例如path传入数组时,表示多个请求路由路径。methods传入数组时,表示多个请求方法。
注:便捷注解传入路由路径
path即可,可以省略键名path,无需传入methods
@RequestMapping(path={"/dddd", "/dddd1"}, methods={"get", "post"})@GetMapping(path={"/get","/get1"})@GetMapping({"/get","/get1"})@PostMapping(path="/post")@PutMapping(path="/put")@DeleteMapping(path="/delete")
php8注解
#[RequestMapping("/test1", methods: "get")]#[RequestMapping(["/test1","/test11"], methods: ["get", "post"])]#[GetMapping(["/get", "/get1"])]#[PostMapping(path: "/post")]#[PutMapping(path: "/put")]#[DeleteMapping("/delete")]
// 方法注解 |
四、示例
|


