Blog Detail

12

Aug
Auto register routes using PHP 8 attributes in Laravel cover image

arrow_back Auto register routes using PHP 8 attributes in Laravel

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. You can view its detailed video by clicking this link.

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="Spatie\RouteAttributes\RouteAttributesServiceProvider" --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 Spatie\RouteAttributes\Attributes\Get;
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 Spatie\RouteAttributes\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 Spatie\RouteAttributes\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);

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 Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\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 apply the Prefix annotation on a class to prefix the routes of all methods of that class.

use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\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 also utilize the Domain annotation on a class to prefix the routes of all methods of that class.

use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\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');

Specifying wheres

You can utilize the Where annotation on a class or method to constrain the format of your route parameters.

use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Where;
use Spatie\RouteAttributes\Attributes\WhereAlphaNumeric;
#[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]+']);

If you wanted to explore more about this package, you can view its detailed documentation on Github.

Published at : 12-08-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project