注解路由

webman 框架 注解路由插件
使用了 doctrine/annotations 包來對代碼內(nèi)的注解進行解析。
您可以直接在控制器類或類方法定義注解,實現(xiàn)路由定義。
webman框架插件地址:http://www.wtbis.cn/plugin/115
站在巨人的肩膀可以看到更遠,感謝 http://www.wtbis.cn/plugin/52 的啟發(fā)。
更新日志
-
v1.1.1
2023-04-11,修復發(fā)現(xiàn)的RequestMapping解析allow_methods問題
-
v1.1.0
2023-03-30,增加php8原生注解支持
-
v1.0.1
2023-03-27,修復發(fā)現(xiàn)的bug
-
v1.0.0
2023-03-27,發(fā)布1.0.0版本
安裝
composer require shayvmo/webman-annotations
使用
配置文件
<?php
// config/plugin/shayvmo/webman-annotations/annotation.php
return [
// 注解掃描路徑, 只掃描應用目錄下已定義的文件夾,例如: app/admin/controller 及其下級目錄
'include_paths' => [
'admin'
],
// requestMapping 允許的請求method
'allow_methods' => ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH'],
// 忽略解析的注解名稱,適用于 php7 使用 doctrine/annotations 解析
'ignored' => [
"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",
]
];
一、中間件注解
注:方法會繼承類定義的中間件。
類和方法通用,參數(shù)中間件類名,單個中間件傳入字符串,多個中間件傳入字符串數(shù)組。
use Shayvmo\WebmanAnnotations\Annotations\Middleware;
use App\third\middleware\SignatureCheckA;
// php74
/**
* @Middleware(
* \App\third\middleware\SignatureCheck::class,
* )
*/
/**
* @Middleware({
* SignatureCheckA::class,
* \App\third\middleware\SignatureCheck::class,
* })
*/
// php8注解
// 單個中間件
#[Middleware(LimitTrafficMiddleware::class)]
// 多個
#[Middleware([LimitTrafficMiddleware::class, \App\third\middleware\SignatureCheck::class])]
二、類注解
類注解有控制器注解@RestController
和資源路由@ResourceMapping
。
資源路由和webman
框架原有的資源路由一致。參考:webman路由
控制器注解
use Shayvmo\WebmanAnnotations\Annotations\RestController;
@RestController
控制器注解,只有一個參數(shù)prefix
,表示整個控制器的路由路徑前綴,方法路由路徑都會拼接該前綴。
傳參可以省略鍵名。
@RestController("/a")
@RestController(prefix="/a")
php8注解
#[RestController("/test1")]
#[RestController(path: "/test2")]
資源路由注解
use Shayvmo\WebmanAnnotations\Annotations\ResourceMapping;
@ResourceMapping
資源路由注解,有path
和 allow_methods
兩個參數(shù)
path
表示資源路由的路徑,allow_methods
為指定的資源方法數(shù)組,不傳指定資源方法時,使用全部資源方法
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
。兩個參數(shù)均可以傳入字符串或數(shù)組。
例如path
傳入數(shù)組時,表示多個請求路由路徑。methods
傳入數(shù)組時,表示多個請求方法。
注:便捷注解傳入路由路徑
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")]
// 方法注解
use Shayvmo\WebmanAnnotations\Annotations\RequestMapping;
use Shayvmo\WebmanAnnotations\Annotations\GetMapping;
use Shayvmo\WebmanAnnotations\Annotations\PostMapping;
use Shayvmo\WebmanAnnotations\Annotations\PutMapping;
use Shayvmo\WebmanAnnotations\Annotations\DeleteMapping;
四、示例
<?php
declare (strict_types=1);
namespace App\third\controller;
use Shayvmo\WebmanAnnotations\Annotations\RestController;
use Shayvmo\WebmanAnnotations\Annotations\DeleteMapping;
use Shayvmo\WebmanAnnotations\Annotations\GetMapping;
use Shayvmo\WebmanAnnotations\Annotations\Middleware;
use Shayvmo\WebmanAnnotations\Annotations\PostMapping;
use Shayvmo\WebmanAnnotations\Annotations\PutMapping;
use Shayvmo\WebmanAnnotations\Annotations\RequestMapping;
use Shayvmo\WebmanAnnotations\Annotations\ResourceMapping;
use App\third\middleware\SignatureCheck;
use support\Request;
use Tinywan\LimitTraffic\Middleware\LimitTrafficMiddleware;
/**
* @RestController("/test")
* @ResourceMapping("/dddd", allow_methods={"index", "show"})
* @Middleware(SignatureCheck::class)
*/
class ATest
{
public function index()
{
//
return 'Test/index';
}
public function show(Request $request, $id)
{
return "Test/show $id";
}
/**
* @GetMapping("/test")
* @Middleware(SignatureCheck::class)
*/
public function get()
{
return 'Test/get';
}
/**
* @RequestMapping(methods={"get", "post"}, path="/test1")
* @Middleware({
* LimitTrafficMiddleware::class,
* })
*/
public function test()
{
return 'Test/test';
}
/**
* @PostMapping("/post")
*/
public function post()
{
return 'Test/post';
}
/**
* @PutMapping("/put")
*/
public function put()
{
return 'Test/put';
}
/**
* @DeleteMapping("/delete")
*/
public function delete()
{
return 'Test/delete';
}
}