As your application grows, route files can become large and hard to maintain. We can split them into separate smaller files. Let's see how to do it.
Option 1: Require/Include Files in routes/web.php
Let's say we have such Controller.
app/Http/Controllers/Account/AccountController.php
namespace App\Http\Controllers\Account; use App\Http\Controllers\Controller;use Illuminate\Http\Request; class AccountController extends Controller{ public function index() { return 'account overview'; }}
Then we can create separate route file for managing user accounts. We can group all routes to encapsulate them with prefixes and middleware.
routes/user/account.php
use App\Http\Controllers\Account\AccountController;use Illuminate\Support\Facades\Route; Route::group([ 'prefix' => 'account', 'as' => 'account.', 'middleware' => 'auth',], function () { Route::get('overview', [AccountController::class, 'index']) ->name('overview');});
And include it in the main web.php
routes file.
routes/web.php
// ... other routes require __DIR__ . '/user/account.php';
You should see the "account overview" text after visiting the /account/overview
path. Laravel Breeze uses this method.
Option 2:
If you need to add only one route file, it can be done in the bootstrap/app.php
file within the withRouting()
methods then
parameter.
bootstrap/app.php:
use Illuminate\Support\Facades\Route; ->withRouting( web: __DIR__.'/../routes/web.php', commands: __DIR__.'/../routes/console.php', health: '/up', then: function () { Route::middleware('web') ->group(base_path('routes/admin/panel.php')); },)
You can also control the whole route registration in the using
parameter.
bootstrap/app.php:
use Illuminate\Support\Facades\Route; ->withRouting( commands: __DIR__.'/../routes/console.php', using: function () { Route::middleware('web') ->group(base_path('routes/web.php')); Route::middleware('web') ->group(base_path('routes/user/account.php')); Route::middleware('web') ->group(base_path('routes/admin/panel.php')); },)
Option 2.1: Load Routes in RouteServiceProvider
NOTICE: This option is for Laravel 10 and below.
Another option is to load them in the RouteServiceProvider
. Let's make a simple Controller real quick.
app/Http/Controllers/Admin/PanelController.php
namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller;use Illuminate\Http\Request; class PanelController extends Controller{ public function index() { return 'panel dashboard'; }}
And define the route in the new file. Note the are no groups.
routes/admin/panel.php
use App\Http\Controllers\Admin\PanelController;use Illuminate\Support\Facades\Route; Route::get('/', [PanelController::class, 'index']);
It is up to you where you want to apply prefixes and middleware to routes. This time we will do it in the RouteServiceProvider
.
app/Providers/RouteServiceProvider.php
public function boot(): void{ // ... $this->routes(function () { Route::middleware('api') ->prefix('api') ->group(base_path('routes/api.php')); Route::middleware('web') ->group(base_path('routes/web.php')); Route::middleware(['web', 'auth']) ->prefix('admin') ->group(base_path('routes/admin/panel.php')); });}
As you can see, we're just repeating the same way as default web.php
and api.php
route files are registered.
No comments or questions yet...