How can route groups let you apply shared configuration, such as a common URL prefix or a set of middleware, to several related routes at once?
Short answer
A route group lets you wrap several related route definitions together, applying shared configuration such as a common URL prefix, a specific middleware that should run for every route within that group, or a shared namespace, all in one place, which significantly reduces…
Advertisement
AModel answer
A route group lets you wrap several related route definitions together, applying shared configuration such as a common URL prefix, a specific middleware that should run for every route within that group, or a shared namespace, all in one place, which significantly reduces repetitive configuration compared to individually specifying the same prefix or middleware on every single related route separately.
Route::prefix('admin')->middleware('auth')->group(function () {
Route::get('/dashboard', [AdminController::class, 'dashboard']);
Route::get('/users', [AdminController::class, 'users']);
});Real-world example
An application groups all of its admin panel routes together under a shared slash admin prefix and a shared authentication middleware, ensuring every single admin route automatically requires login without needing to repeat that configuration for each individual route.
Likely follow-ups
- Can route groups be nested within other route groups?
- How do multiple middleware specified on the same group combine together?
Related topics