annotation注解路由

2.0.1
版本
2024-03-21
版本更新時(shí)間
684
安裝
19
star
webman annotation 注解
使用了
doctrine/annotations
包來對(duì)代碼內(nèi)的注解進(jìn)行解析。支持php8注解方式
您可以直接在控制器類任意方法定義
@RequestMapping
注解來完成一個(gè)路由的定義,如需使用路由中間件請(qǐng)定義該路由的注解方法@Middwares
或@Middware
注解并引入中間件命名空間即可感謝各位大佬批評(píng)指正,不足之處,還望海涵!
安裝
composer require sunsgne/annotations
使用
路由控制
- GET
- POST
- PUT
- DELETE
- HEADER
- OPTIONS
use Sunsgne\Annotations\Mapping\RequestMapping; /** * 允許通過 GET 或 POST 方式請(qǐng)求 * @RequestMapping(methods="GET , POST" , path="/api/json") * @param Request $request * @return Response */ public function json(Request $request) { return json(['code' => 0, 'msg' => 'ok']); }
路由中間件
在通過注解定義路由時(shí),您僅可通過注解的方式來定義中間件,對(duì)中間件的定義有兩個(gè)注解,分別為:
使用
@Middleware
注解時(shí)需use Sunsgne\Annotations\Mapping\Middleware;
命名空間;使用
@Middlewares
注解時(shí)需use Sunsgne\Annotations\Mapping\Middlewares;
命名空間;
@Middleware
注解為定義單個(gè)中間件時(shí)使用,在一個(gè)地方僅可定義一個(gè)該注解,不可重復(fù)定義@Middlewares
注解為定義多個(gè)中間件時(shí)使用,在一個(gè)地方僅可定義一個(gè)該注解,然后通過在該注解內(nèi)定義多個(gè)@Middleware
注解實(shí)現(xiàn)多個(gè)中間件的定義
定義單個(gè)中間件:
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']);
}
定義多個(gè)中間件:
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+版本
*注意請(qǐng)勿直接copy。示例中未創(chuàng)建中間件
-
定義路由
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']); }
-
定義路由并配置中間件(多個(gè))
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']); }
忽略注解參數(shù)
請(qǐng)?jiān)?code>config/plugin/sunsgne/annotations/ignored文件中添加需要忽略的參數(shù)
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
- 修復(fù)注解含有
混雜參數(shù)
,導(dǎo)致讀取失敗的問題 - 新增配置文件
ignored.php
,用于對(duì)注解中的其他參數(shù)做忽略讀取操作:如datetime
,used
等。 - 對(duì)
php8
以上版本做原生注解
的適配