PHPackages                             bfg/route - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. bfg/route

ActiveLibrary[API Development](/categories/api)

bfg/route
=========

Auto register routes using PHP attributes

1.2.5(2y ago)11571MITPHPPHP ^8.0

Since Jan 26Pushed 2y ago2 watchersCompare

[ Source](https://github.com/bfg-s/route)[ Packagist](https://packagist.org/packages/bfg/route)[ Docs](https://github.com/bfg/route)[ RSS](/packages/bfg-route/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (6)Versions (19)Used By (0)

Use PHP 8 attributes to register routes in a Laravel app
========================================================

[](#use-php-8-attributes-to-register-routes-in-a-laravel-app)

[![Latest Version on Packagist](https://camo.githubusercontent.com/324da8b6e3f695869a790d9f85f5373839a289c4a599a871d3e174d4d709f6d4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6266672f726f7574652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bfg-s/route)

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
------------

[](#installation)

You can install the package via composer:

```
composer require bfg/route
```

Usage
-----

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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
----------

[](#deployment)

As stated in the documentation which you can see [here](https://laravel.com/docs/8.x/deployment#optimizing-route-loading). After you cache your routes ...

```
php artisan route:cache
```

... scanning of your classes will be disabled.

Testing
-------

[](#testing)

```
cd vendor/bfg/route
composer install
composer test
```

Inspired by
-----------

[](#inspired-by)

I took [this](https://github.com/spatie/laravel-route-attributes) 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
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~65 days

Recently: every ~190 days

Total

18

Last Release

825d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/59b2d162a30938ac2c3c56340ebea07a6778a3e1c86cb70b5bc28b69a1c3f04d?d=identicon)[bfg](/maintainers/bfg)

---

Top Contributors

[![Xsaven](https://avatars.githubusercontent.com/u/1726771?v=4)](https://github.com/Xsaven "Xsaven (22 commits)")

---

Tags

spatielaravelattributesroutebfg

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bfg-route/health.svg)

```
[![Health](https://phpackages.com/badges/bfg-route/health.svg)](https://phpackages.com/packages/bfg-route)
```

###  Alternatives

[spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

1.9k15.1M99](/packages/spatie-laravel-fractal)[scalar/laravel

Render your OpenAPI-based API reference

6183.9k2](/packages/scalar-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
