bfg/route is a Laravel package for auto register routes using php attributes.
It currently has 1 GitHub stars and 159 downloads on Packagist (latest version 1.2.5).
Install it with composer require bfg/route.
Discover more Laravel packages by bfg
or browse all Laravel packages to compare alternatives.
Last updated
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']);
You can install the package via composer:
composer require bfg/route
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
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']);
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')]
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');
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]);
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']);
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');
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.
cd vendor/bfg/route
composer install
composer test
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.
The MIT License (MIT). Please see License File for more information.