php 30 lines · 8 steps

How nested route groups compose in Laravel

A single admin route group layers shared middleware, URL prefixes, and name prefixes onto every nested route.

Explained by highlit
1use Illuminate\Support\Facades\Route;
2use App\Http\Controllers\Admin\DashboardController;
3use App\Http\Controllers\Admin\UserController;
4use App\Http\Controllers\Admin\ReportController;
5 
6Route::middleware(['auth', 'verified', 'can:access-admin'])
7 ->prefix('admin')
8 ->name('admin.')
9 ->group(function () {
10 Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
11 
12 Route::prefix('users')->name('users.')->group(function () {
13 Route::get('/', [UserController::class, 'index'])->name('index');
14 Route::get('/create', [UserController::class, 'create'])->name('create');
15 Route::post('/', [UserController::class, 'store'])->name('store');
16 Route::get('/{user}', [UserController::class, 'show'])->name('show');
17 Route::get('/{user}/edit', [UserController::class, 'edit'])->name('edit');
18 Route::put('/{user}', [UserController::class, 'update'])->name('update');
19 Route::delete('/{user}', [UserController::class, 'destroy'])->name('destroy');
20 });
21 
22 Route::controller(ReportController::class)
23 ->prefix('reports')
24 ->name('reports.')
25 ->group(function () {
26 Route::get('/', 'index')->name('index');
27 Route::get('/revenue', 'revenue')->name('revenue');
28 Route::get('/export', 'export')->name('export');
29 });
30 });
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Route groups let you declare cross-cutting concerns like auth and prefixes once instead of repeating them per route.
  2. 2Nesting groups makes prefixes and name segments accumulate, producing paths like /admin/users and names like admin.users.index.
  3. 3Route::controller binds a controller to a group so nested routes reference only the method name.

Related explainers

Share this explainer

Here's the card — post it anywhere.

How nested route groups compose in Laravel — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code