Route Groups, Prefixes & Namespaces
Route groups share attributes — middleware, URL prefixes, name prefixes — across multiple routes without repetition. They're the primary organizational tool for managing authentication boundaries, API versions, admin panels, and feature-specific route sections. Well-organized routes make your application dramatically easier to navigate and reason about as it grows.
Route Groups
// Admin panel with middleware + prefix + name prefix
Route::middleware(['auth', 'admin'])
->prefix('admin')
->name('admin.')
->group(function () {
Route::resource('users', AdminUserController::class);
Route::resource('posts', AdminPostController::class);
Route::get('/stats', [AdminStatsController::class, 'index'])->name('stats');
});
// Routes created:
// GET /admin/users → admin.users.index
// GET /admin/posts → admin.posts.index
// GET /admin/stats → admin.stats
// All protected by auth + admin middleware
// API versioning
Route::prefix('v1')->name('api.v1.')->group(function () {
Route::apiResource('users', V1\UserController::class);
Route::apiResource('posts', V1\PostController::class);
});
Route::prefix('v2')->name('api.v2.')->group(function () {
Route::apiResource('users', V2\UserController::class);
});
// /api/v1/users vs /api/v2/usersWhy Route Groups Matter
- DRY principle: Without groups, you'd repeat ->middleware('auth') on every protected route. Groups apply it once to hundreds of routes simultaneously
- Security: Define authentication boundaries explicitly. All routes inside a group inherit its middleware — it's impossible to accidentally add an unprotected route inside an authenticated group
- Organization: Split routes by domain (admin, api, public), feature (billing, posts, users), or version (v1, v2). This makes routes/web.php readable even with 100+ routes
- Name prefixing: admin. prefix means all admin routes are auto-named with admin. — the route list (php artisan route:list) shows clear domain separation
- Controller namespacing: ->namespace('Admin') groups routes to a subdirectory without repeating the namespace on every controller class reference
Tip
Tip
Practice Route Groups Prefixes Namespaces in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Define the relationship, Eloquent builds the SQL automatically
Practice Task
Note
Practice Task — (1) Write a working example of Route Groups Prefixes Namespaces from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Route Groups Prefixes Namespaces is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.
Key Takeaways
- Route groups share attributes — middleware, URL prefixes, name prefixes — across multiple routes without repetition.
- DRY principle: Without groups, you'd repeat ->middleware('auth') on every protected route. Groups apply it once to hundreds of routes simultaneously
- Security: Define authentication boundaries explicitly. All routes inside a group inherit its middleware — it's impossible to accidentally add an unprotected route inside an authenticated group
- Organization: Split routes by domain (admin, api, public), feature (billing, posts, users), or version (v1, v2). This makes routes/web.php readable even with 100+ routes