PHPackages                             micaeldias/laravel-single-file-routes - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. micaeldias/laravel-single-file-routes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

micaeldias/laravel-single-file-routes
=====================================

Streamlined route management for your Laravel applications.

v3.0.0(2y ago)73[2 PRs](https://github.com/micaeldias/laravel-single-file-routes/pulls)MITPHPPHP ^8.0

Since Oct 29Pushed 2y ago1 watchersCompare

[ Source](https://github.com/micaeldias/laravel-single-file-routes)[ Packagist](https://packagist.org/packages/micaeldias/laravel-single-file-routes)[ Docs](https://github.com/micaeldias/laravel-single-file-routes)[ RSS](/packages/micaeldias-laravel-single-file-routes/feed)WikiDiscussions main Synced today

READMEChangelog (3)Dependencies (14)Versions (9)Used By (0)

Single file routes for Laravel
==============================

[](#single-file-routes-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c873b4eaa6eab232cf7384d4df32d20b7c0deedbe3cd797e5cf2cd4c9eeda62d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d696361656c646961732f6c61726176656c2d73696e676c652d66696c652d726f757465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/micaeldias/laravel-single-file-routes)[![GitHub Tests Action Status](https://camo.githubusercontent.com/0385e1f3c9622ec82616b5685f3e190cac111a3fc3e0784e4e332b2b1485fe42/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d696361656c646961732f6c61726176656c2d73696e676c652d66696c652d726f757465732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/micaeldias/laravel-single-file-routes/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/914a037fd9d5bb66928b10c15322b3bdb95d1be85f4239810f0dd46beb5da7e9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d696361656c646961732f6c61726176656c2d73696e676c652d66696c652d726f757465732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/micaeldias/laravel-single-file-routes/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/7a6add24849ebe6a786e18de3b9b469dc4ca23a6c02f4cba3ccfe7f1a4081325/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d696361656c646961732f6c61726176656c2d73696e676c652d66696c652d726f757465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/micaeldias/laravel-single-file-routes)

Single file routes allows you to co-locate everything about a route into a single file. See the route method, URI, middleware, and it's behaviour at a glance without the need to keep track of multiple files.

```
namespace App\Http\Routes\Api\User;

use App\Http\ApiRouteGroup;
use Illuminate\Http\Request;
use MicaelDias\SingleFileRoutes\Routing\Route;

#[Route(method: 'GET', uri: '/user/{id}', group: ApiRouteGroup::class)]
class Get
{
    /**
     * Handle the request.
     */
    public function __invoke(Request $request, int $id)
    {
        // return view('user', ['id' => $id]);
        // return new JsonResponse(['id' => $id]);
        // or any other response supported by Laravel.
    }
}
```

You can still register routes like you're used to in conjunction with this package, this is not an all-in package.

Compatibility
-------------

[](#compatibility)

Laravel versionPHP versionPackage version87.4 - 8.119 - 108.0 - 8.229 - 108.0 - 8.23Installation
------------

[](#installation)

You may install single file routes into your project with:

```
composer require micaeldias/laravel-single-file-routes
```

After installing, publish the assets using the `single-file-routes:install` Artisan command:

```
php artisan single-file-routes:install
```

After publishing single file routes' assets, its primary configuration file will be located at `config/single-file-routes.php`.

Usage
-----

[](#usage)

### Single Action Controllers

[](#single-action-controllers)

To register a route in a single action controller, simply add the `Route` attribute to the class itself:

```
#[Route(method: 'GET', uri: '/user/{id}')]
class Get {
    __invoke (Request $request, int $id) {
        // Handle the request
    }
}
```

### Basic Controllers

[](#basic-controllers)

To register a route in a basic controller, you need to the `Route` attribute to the method instead:

```
class UserController
{
    #[Route(method: 'GET', uri: '/user/{id}')]
    public function show(int $id): View
    {
        return view('user.profile', [
            'user' => User::findOrFail($id)
        ]);
    }
}
```

### Route Groups

[](#route-groups)

To use a route group, you first need to create one. This is easily done using the artisan command `make:route-group` explained below.

The route group extends the following interface:

```
interface RouteGroup
{
    /**
     * The URI prefix for all routes on this group.
     */
    public static function prefix(): string;

    /**
     * The middleware used for all routes on this group.
     */
    public static function middleware(): array;

    /**
     * Assign this route group to a subdomain.
     */
    public static function domain(): ?string;
}
```

Once you've created your desired route group, you can start adding routes under it. Let's say we create an `ApiRouteGroup`, to use it on a route we can simply pass the `group` param on the `Route` attribute, as such:

```
#[Route(method: 'GET', uri: '/user/{id}', group: ApiRouteGroup::class)]
```

### Route names

[](#route-names)

By default, routes' names are the same as their definition meaning you can simply pass the class name in Laravel's `route` method to get the URL.

#### Single Action Controllers

[](#single-action-controllers-1)

```
use App\Http\Routes\Api\User\Get as UserGet;

route(UserGet::class) # http://localhost/api/user
```

#### Basic Controllers

[](#basic-controllers-1)

```
route('App\Http\Controllers\UserController::index()')
```

#### Custom Route Names

[](#custom-route-names)

If you don't like having the route name the same as it's definition you can pass the `name` param on the `Route` attribute:

```
#[Route(method: 'GET', uri: '/user/{id}', name: 'users.show')]
```

### Adding middleware

[](#adding-middleware)

You can use any middleware supported by Laravel directly in the route or on route groups to be used by all routes under it.

#### Route Group Middleware

[](#route-group-middleware)

To add middleware to the route group, return it from the `middleware()` function:

```
public static function middleware(): array
{
    return [
        Authenticate::class,
        'throttle:30,1',
    ];
}
```

#### Specific Route Middleware

[](#specific-route-middleware)

If you need to add middleware to a single Route, you can pass the `middleware` param on the `Route` attribute:

```
#[Route(method: 'PUT', uri: '/post/{id}', middleware: ['can:update,post'])]
```

### Multiple Routes

[](#multiple-routes)

If for some reason you need a controller to respond to multiple URIs, you can stack as many Routes as needed:

```
#[Route(method: 'GET', uri: '/user', group: ApiRouteGroup::class)]
#[Route(method: 'GET', uri: '/users', group: ApiRouteGroup::class)]
public function show(int $id): View
```

This will make `/user` and `/users` have the same behaviour.

### Route Caching

[](#route-caching)

Laravel's route caching is fully supported.

In fact, due to how routes are discovered, it's highly recommended that you do [cache your routes](https://laravel.com/docs/master/routing#route-caching) in production.

Artisan Commands
----------------

[](#artisan-commands)

### Generate Route Groups

[](#generate-route-groups)

You can easily create a route group using the `make:route-group` Artisan command:

```
php artisan make:route-group {name} {?prefix}
```

Let's say we want to create an API route group:

```
php artisan make:route-group ApiRouteGroup /api
# INFO  Route Group [app/Http/Routes/ApiRouteGroup.php] created successfully.
```

Here's how the generated class would look like:

```
namespace App\Http\Routes;

use MicaelDias\SingleFileRoutes\Routing\RouteGroup;

class ApiRouteGroup implements RouteGroup
{
    /**
     * {@inheritdoc}
     */
    public static function prefix(): string
    {
        return '/api';
    }

    /**
     * {@inheritdoc}
     */
    public static function middleware(): array
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function domain(): ?string
    {
        return null;
    }
}
```

### Generate Routes

[](#generate-routes)

We only support generating single action controllers using the `make:route` command.

Follow the prompts to generate the desired route:

```
php artisan make:route
```

[![](assets/make-route.gif)](assets/make-route.gif)

Here's how the generated class would look like:

```
namespace App\Http\Routes\Api\User;

use Illuminate\Http\Request;
use MicaelDias\SingleFileRoutes\Routing\Route;
use App\Http\Routes\ApiRouteGroup;

#[Route(method: 'GET', uri: '/user', group: ApiRouteGroup::class)]
class Get
{
    /**
     * Handle the request.
     */
    public function __invoke(Request $request)
    {
        // @todo handle request
    }
}
```

Keep in mind that the route generators were created for your convenience, but you're of course free to manually create routes or groups if it's simpler for your use-case.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Micael Dias](https://github.com/micaeldias)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 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 ~1 days

Total

6

Last Release

975d ago

Major Versions

v1.x-dev → v2.0.02023-10-29

v2.x-dev → v3.0.02023-11-01

PHP version history (2 changes)v1.0.0PHP ^7.4|^8.0

v2.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3e4ff27caedae693692f95622a21577df96039d598bb74877a5d212f0dbba2ce?d=identicon)[micaeldias](/maintainers/micaeldias)

---

Top Contributors

[![micaeldias](https://avatars.githubusercontent.com/u/14938386?v=4)](https://github.com/micaeldias "micaeldias (8 commits)")

---

Tags

laravelmicaeldiaslaravel-single-file-routes

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/micaeldias-laravel-single-file-routes/health.svg)

```
[![Health](https://phpackages.com/badges/micaeldias-laravel-single-file-routes/health.svg)](https://phpackages.com/packages/micaeldias-laravel-single-file-routes)
```

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329530.5k29](/packages/codewithdennis-filament-select-tree)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

124603.0k](/packages/worksome-exchange)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[tarfin-labs/event-machine

Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.

199.4k](/packages/tarfin-labs-event-machine)[tapp/filament-form-builder

User facing form builder using Filament components

132.4k3](/packages/tapp-filament-form-builder)

PHPackages © 2026

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