Separate Routes by Files

If you have a set of routes related to a certain "section", you may separate them in a special routes/XXXXX.php file, and just include it in routes/web.php

Example with routes/auth.php in Laravel Breeze by Taylor Otwell himself:

1Route::get('/', function () {
2 return view('welcome');
3});
4 
5Route::get('/dashboard', function () {
6 return view('dashboard');
7})->middleware(['auth'])->name('dashboard');
8 
9require __DIR__.'/auth.php';

Then, in routes/auth.php:

1use App\Http\Controllers\Auth\AuthenticatedSessionController;
2use App\Http\Controllers\Auth\RegisteredUserController;
3// ... more controllers
4 
5use Illuminate\Support\Facades\Route;
6 
7Route::get('/register', [RegisteredUserController::class, 'create'])
8 ->middleware('guest')
9 ->name('register');
10 
11Route::post('/register', [RegisteredUserController::class, 'store'])
12 ->middleware('guest');
13 
14// ... A dozen more routes

But you should use this include() only when that separate route file has the same settings for prefix/middlewares, otherwise it's better to group them in app/Providers/RouteServiceProvider:

1public function boot()
2{
3 $this->configureRateLimiting();
4 
5 $this->routes(function () {
6 Route::prefix('api')
7 ->middleware('api')
8 ->namespace($this->namespace)
9 ->group(base_path('routes/api.php'));
10 
11 Route::middleware('web')
12 ->namespace($this->namespace)
13 ->group(base_path('routes/web.php'));
14 
15 // ... Your routes file listed next here
16 });
17}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord