How to Use Route Groups in Laravel for Cleaner Routing
In Laravel, use
Route::group() to group routes that share common attributes like middleware, prefix, or namespace. This helps keep your routes organized and reduces repetition by applying settings to multiple routes at once.Syntax
The Route::group() method accepts an array of attributes and a closure containing the grouped routes. Common attributes include middleware, prefix, and namespace.
- middleware: Applies middleware to all routes in the group.
- prefix: Adds a URL prefix to all routes.
- namespace: Sets the controller namespace for all routes.
php
Route::group(['middleware' => 'auth', 'prefix' => 'admin', 'namespace' => 'Admin'], function () { Route::get('/dashboard', 'DashboardController@index'); Route::get('/users', 'UserController@index'); });
Example
This example shows a route group with a prefix and middleware. All routes inside the group will have URLs starting with /admin and require the auth middleware.
php
<?php use Illuminate\Support\Facades\Route; Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () { Route::get('/dashboard', function () { return 'Admin Dashboard'; }); Route::get('/users', function () { return 'User List'; }); });
Output
Visiting /admin/dashboard shows 'Admin Dashboard'.
Visiting /admin/users shows 'User List'.
Both routes require authentication middleware.
Common Pitfalls
Common mistakes when using route groups include:
- Forgetting to include the leading slash in route URIs inside the group, which can cause unexpected URLs.
- Misusing
namespacein Laravel 8+ where default namespace prefixing is removed; you may need to specify full controller class names. - Applying middleware incorrectly or forgetting to register custom middleware.
php
<?php // Wrong: Missing prefix causes routes to be at root instead of /admin Route::group(['prefix' => 'admin'], function () { Route::get('dashboard', function () { return 'Dashboard'; }); }); // Right: Add leading slash or no slash consistently Route::group(['prefix' => 'admin'], function () { Route::get('dashboard', function () { return 'Dashboard'; }); });
Quick Reference
| Attribute | Description | Example |
|---|---|---|
| middleware | Apply middleware to all routes in the group | 'middleware' => 'auth' |
| prefix | Add URL prefix to all routes | 'prefix' => 'admin' |
| namespace | Set controller namespace (Laravel 7 and below) | 'namespace' => 'Admin' |
| as | Prefix route names | 'as' => 'admin.' |
| where | Apply constraints on route parameters | 'where' => ['id' => '[0-9]+'] |
Key Takeaways
Use Route::group() to apply shared attributes like middleware and prefixes to multiple routes.
Always include leading slashes consistently in route URIs inside groups to avoid URL issues.
In Laravel 8+, specify full controller class names instead of relying on namespace attribute.
Route groups help keep your routing file clean and DRY by reducing repetition.
Test grouped routes to ensure middleware and prefixes apply as expected.