bfg / route
Auto register routes using PHP attributes
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
This package provides annotations to automatically register routes. Here's a quick example:
use Bfg\Route\Attributes\Get; use Bfg\Route\Attributes\Resource; class MyController { #[Get('my-route')] public function myMethod() { } } #[Resource('my_resource')] class MyResourceController { ... }
This attribute will automatically register this route:
Route::get('my-route', [MyController::class, 'myMethod']);
Installation
You can install the package via composer:
composer require bfg/route
Usage
In your RouteServiceProvider
, delete where your controllers are located and he will do the rest for you:
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') ); }); }
The package provides several annotations that should be put on controller classes and methods. These annotations will be used to automatically register routes
Adding a GET route
use Bfg\Route\Attributes\Get; class MyController { #[Get('my-route')] public function myMethod() { } }
This attribute will automatically register this route:
Route::get('my-route', [MyController::class, 'myMethod']);
Using other HTTP verbs
We have left no HTTP verb behind. You can use these attributes on controller methods.
#[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')]
Specify a route name
All HTTP verb attributes accept a parameter named name
that accepts a route name.
use Bfg\Route\Attributes\Get; class MyController { #[Get('my-route', name: "my-route-name")] public function myMethod() { } }
This attribute will automatically register this route:
Route::get('my-route', [MyController::class, 'myMethod'])->name('my-route-name');
Adding middleware
All HTTP verb attributes accept a parameter named middleware
that accepts a middleware class or an array of middleware classes.
use Bfg\Route\Attributes\Get; class MyController { #[Get('my-route', middleware: MyMiddleware::class)] public function myMethod() { } }
This annotation will automatically register this route:
Route::get('my-route', [MyController::class, 'myMethod'])->middleware(MyMiddleware::class);
To apply middleware on all methods of a class you can use the Middleware
attribute. You can mix this with applying attribute on a method.
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() { } }
These annotations will automatically register these routes:
Route::get('my-route', [MyController::class, 'firstMethod'])->middleware(MyMiddleware::class); Route::get('my-other-route', [MyController::class, 'secondMethod'])->middleware([MyMiddleware::class, MyOtherMiddleware]);
Specifying a prefix
You can use the Prefix
annotation on a class to prefix the routes of all methods of that class.
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() { } }
These annotations will automatically register these routes:
Route::get('my-prefix/my-get-route', [MyController::class, 'myGetMethod']); Route::post('my-prefix/my-post-route', [MyController::class, 'myPostMethod']);
Specifying a domain
You can use the Domain
annotation on a class to prefix the routes of all methods of that class.
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() { } }
These annotations will automatically register these routes:
Route::get('my-get-route', [MyController::class, 'myGetMethod'])->domain('my-subdomain.localhost'); Route::post('my-post-route', [MyController::class, 'myPostMethod'])->domain('my-subdomain.localhost');
Deployment
As stated in the documentation which you can see here. After you cache your routes ...
php artisan route:cache
... scanning of your classes will be disabled.
Testing
cd vendor/bfg/route composer install composer test
Inspired by
I took this package into the service and reworked it a little, added a couple of functions, caching and added the ability to extend it a little, I plan to support a more advanced API as far as possible.
License
The MIT License (MIT). Please see License File for more information.