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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Route groups let you declare cross-cutting concerns like auth and prefixes once instead of repeating them per route.
- 2Nesting groups makes prefixes and name segments accumulate, producing paths like /admin/users and names like admin.users.index.
- 3Route::controller binds a controller to a group so nested routes reference only the method name.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-nested-route-groups-compose-in-laravel-explained-php-ce19/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.