Skip to content

Add RegistersRouteFiles trait to ServiceProvider#59152

Draft
garanaibrahim7 wants to merge 2 commits intolaravel:12.xfrom
garanaibrahim7:feature/registers-route-files-trait
Draft

Add RegistersRouteFiles trait to ServiceProvider#59152
garanaibrahim7 wants to merge 2 commits intolaravel:12.xfrom
garanaibrahim7:feature/registers-route-files-trait

Conversation

@garanaibrahim7
Copy link

What this PR does

Adds a RegistersRouteFiles trait to ServiceProvider that provides
a fluent, cache-compatible API for loading route files without manual
require calls or repetitive Route::group() boilerplate.

Usage

$this->registerRouteFiles('api');
$this->registerRouteFiles(['web', 'api', 'admin']);
$this->registerRouteFiles('api/v2', [
'middleware' => ['api', 'auth:sanctum'],
'prefix' => 'api/v2',
]);

Why

  • No bare require — fully compatible with php artisan route:cache
  • Deduplication across multiple ServiceProviders
  • Resolves relative and absolute paths automatically
  • Fluent chaining supported

@garanaibrahim7 garanaibrahim7 marked this pull request as draft March 11, 2026 06:41
@rodrigopedra
Copy link
Contributor

rodrigopedra commented Mar 12, 2026

So instead of calling:

$router->middleware(['api', 'auth:sanctum'])
    ->prefix('/api')
    ->as('api.')
    ->group($app->basePath('routes/api.php'));

Your proposal is this would be more "fluent" or straightforward?

$router->registerRouteFiles('api', [
    'middleware' => ['api', 'auth:sanctum'],
    'prefix' => '/api',
    'as' => 'api.',
]);

Are there any other benefits that would counter the loss of IDE autocompletion, for example?

Mind that $app and $router are passed down when configuring routing with a closure on bootstrap/app.php:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Routing\Router;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(function (Application $app, Router $router) {
        $router->middleware(['api', 'auth:sanctum'])
            ->prefix('/api')
            ->as('api.')
            ->group($app->basePath('routes/api.php'));

        $router->middleware('web')
            ->group($app->basePath('routes/web.php'));
    })
    ->withCommands([dirname(__DIR__) . '/routes/console.php'])
    ->withMiddleware(function (Middleware $middleware): void {
        //
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants