
Spatie has introduced a laravel-route-attributes package that provides a way to automatically register the routes utilizing PHP 8 attributes in laravel. This package provides several annotations to automatically register the routes. This package requires the minimum php 8 version.
Here you’ll get an introduction to PHP 8 attributes and how these laravel-routes-attributes operate under the hood.
Installation
You can install this package via composer by running this command:
composer require spatie/laravel-route-attributes
Next You’ve to publish the config file by running this command:
php artisan vendor:publish --provider="SpatieRouteAttributesRouteAttributesServiceProvider" --tag="config"
This is the contents of the published config file:
return [
/*
* Automatic registration of routes will only happen if this setting is `true`
*/
'enabled' => true,
/*
* Controllers in these directories that have routing attributes
* will automatically be registered.
*/
'directories' => [
app_path('Http/Controllers'),
],
];
Usage
The package provides several annotations that should be put on controller classes and methods. These annotations will be utilized to automatically register routes.
Adding a GET route
use SpatieRouteAttributesAttributesGet;
class MyController
{
#[Get('my-route')]
public function myMethod()
{
}
}
This attribute will automatically register this route:
Route::get('my-route', [MyController::class, 'myMethod']);
Specify a route name
All HTTP verb attributes accept a parameter named name that accepts a route name.
use SpatieRouteAttributesAttributesGet;
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 SpatieRouteAttributesAttributesGet;
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);
If you wanted to apply middleware to all methods of a class you can utilize the Middleware attribute. You can mix this with applying attributes to a method.
use SpatieRouteAttributesAttributesGet;
use SpatieRouteAttributesAttributesMiddleware;
#[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 apply the Prefix annotation on a class to prefix the routes of all methods of that class.
use SpatieRouteAttributesAttributesGet;
use SpatieRouteAttributesAttributesPost;
use SpatieRouteAttributesAttributesPrefix;
#[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 also utilize the Domain annotation on a class to prefix the routes of all methods of that class.
use SpatieRouteAttributesAttributesGet;
use SpatieRouteAttributesAttributesPost;
use SpatieRouteAttributesAttributesDomain;
#[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');
Specifying wheres
You can utilize the Where annotation on a class or method to constrain the format of your route parameters.
use SpatieRouteAttributesAttributesGet;
use SpatieRouteAttributesAttributesPost;
use SpatieRouteAttributesAttributesWhere;
use SpatieRouteAttributesAttributesWhereAlphaNumeric;
#[Where('my-where', '[0-9]+')]
class MyController
{
#[Get('my-get-route/{my-where}')]
public function myGetMethod()
{
}
#[Post('my-post-route/{my-where}/{my-alpha-numeric}')]
#[WhereAlphaNumeric('my-alpha-numeric')]
public function myPostMethod()
{
}
}
These annotations will automatically register these routes:
Route::get('my-get-route/{my-where}', [MyController::class, 'myGetMethod'])->where(['my-where' => '[0-9]+']);
Route::post('my-post-route/{my-where}/{my-alpha-numeric}', [MyController::class, 'myPostMethod'])->where(['my-where' => '[0-9]+', 'my-alpha-numeric' => '[a-zA-Z0-9]+']);
