bfg / route
使用 PHP 属性自动注册路由
1.2.5
2024-02-11 13:40 UTC
Requires
- php: ^8.0
- bfg/entity: *
- illuminate/contracts: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.3
- psalm/plugin-laravel: ^1.4
- vimeo/psalm: ^4.1
README
此包提供注解以自动注册路由。以下是一个快速示例
use Bfg\Route\Attributes\Get; use Bfg\Route\Attributes\Resource; class MyController { #[Get('my-route')] public function myMethod() { } } #[Resource('my_resource')] class MyResourceController { ... }
此属性将自动注册此路由
Route::get('my-route', [MyController::class, 'myMethod']);
安装
您可以通过 composer 安装此包
composer require bfg/route
用法
在您的 RouteServiceProvider
中,删除控制器所在位置,它将为您完成剩余的工作
public function boot() { $this->configureRateLimiting(); $this->routes(function () { Route::find( // Path for search attributes, // you can use class namespaces, // directories and file paths __DIR__ . '/../Http/Controllers', // Here you can transfer the parent // instance of the route from which // the nesting will be created. Route::middleware('web') ); }); }
该包提供了一些应该在控制器类和方法上使用的注解。这些注解将用于自动注册路由
添加 GET 路由
use Bfg\Route\Attributes\Get; class MyController { #[Get('my-route')] public function myMethod() { } }
此属性将自动注册此路由
Route::get('my-route', [MyController::class, 'myMethod']);
使用其他 HTTP 动词
我们没有遗漏任何 HTTP 动词。您可以在控制器方法上使用这些属性。
#[Bfg\Route\Attributes\Post('my-uri')] #[Bfg\Route\Attributes\Put('my-uri')] #[Bfg\Route\Attributes\Patch('my-uri')] #[Bfg\Route\Attributes\Delete('my-uri')] #[Bfg\Route\Attributes\Options('my-uri')]
指定路由名称
所有 HTTP 动词属性都接受一个名为 name
的参数,该参数接受一个路由名称。
use Bfg\Route\Attributes\Get; class MyController { #[Get('my-route', name: "my-route-name")] public function myMethod() { } }
此属性将自动注册此路由
Route::get('my-route', [MyController::class, 'myMethod'])->name('my-route-name');
添加中间件
所有 HTTP 动词属性都接受一个名为 middleware
的参数,该参数接受一个中间件类或中间件类数组。
use Bfg\Route\Attributes\Get; class MyController { #[Get('my-route', middleware: MyMiddleware::class)] public function myMethod() { } }
此注解将自动注册此路由
Route::get('my-route', [MyController::class, 'myMethod'])->middleware(MyMiddleware::class);
要在一个类的所有方法上应用中间件,您可以使用 Middleware
注解。您可以将此与在方法上应用属性结合起来使用。
use Bfg\Route\Attributes\Get; use Bfg\Route\Attributes\Middleware; #[Middleware(MyMiddleware::class)] class MyController { #[Get('my-route')] public function firstMethod() { } #[Get('my-other-route', middleware: MyOtherMiddleware::class)] public function secondMethod() { } }
这些注解将自动注册这些路由
Route::get('my-route', [MyController::class, 'firstMethod'])->middleware(MyMiddleware::class); Route::get('my-other-route', [MyController::class, 'secondMethod'])->middleware([MyMiddleware::class, MyOtherMiddleware]);
指定前缀
您可以在类上使用 Prefix
注解来为该类所有方法的路由添加前缀。
use Bfg\Route\Attributes\Get; use Bfg\Route\Attributes\Post; use Bfg\Route\Attributes\Prefix; #[Prefix('my-prefix')] class MyController { #[Get('my-get-route')] public function myGetMethod() { } #[Post('my-post-route')] public function myPostMethod() { } }
这些注解将自动注册这些路由
Route::get('my-prefix/my-get-route', [MyController::class, 'myGetMethod']); Route::post('my-prefix/my-post-route', [MyController::class, 'myPostMethod']);
指定域名
您可以在类上使用 Domain
注解来为该类所有方法的路由添加域名前缀。
use Bfg\Route\Attributes\Get; use Bfg\Route\Attributes\Post; use Bfg\Route\Attributes\Domain; #[Domain('my-subdomain.localhost')] class MyController { #[Get('my-get-route')] public function myGetMethod() { } #[Post('my-post-route')] public function myPostMethod() { } }
这些注解将自动注册这些路由
Route::get('my-get-route', [MyController::class, 'myGetMethod'])->domain('my-subdomain.localhost'); Route::post('my-post-route', [MyController::class, 'myPostMethod'])->domain('my-subdomain.localhost');
部署
如文档所述,您可以在 此处 查看。在您缓存了路由之后...
php artisan route:cache
...将禁用您的类扫描。
测试
cd vendor/bfg/route composer install composer test
灵感来源
我采用了 此 包并将其融入到服务中,进行了一些修改,添加了几个功能,实现了缓存,并添加了扩展功能,我计划尽可能地支持更高级的 API。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。