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
javascript
const crypto = require('crypto'); function securityHeaders(options = {}) { const {
A configurable security-headers middleware in Express
middleware
http-headers
content-security-policy
Intermediate
7 steps
go
package middleware import ( "bytes"
Verifying webhook HMAC signatures in Gin
hmac
middleware
webhooks
Intermediate
7 steps
php
final class TokenBucketLimiter { private float $tokens; private float $lastRefill;
How a token bucket rate limiter works
rate-limiting
token-bucket
throttling
Intermediate
7 steps
ruby
class ArticlesController < ApplicationController before_action :set_article, only: %i[show edit update destroy publish] def edit
Policy-based authorization in a Rails controller
authorization
policy-object
before-action
Intermediate
9 steps
go
package server import ( "net/http"
Serving a single-page app with Gin
static-files
spa
routing
Intermediate
7 steps
php
<?php namespace App\Events;
How a broadcast event works in Laravel
broadcasting
websockets
events
Intermediate
5 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.