Route groups allow you to organize multiple routes and apply shared settings such as URL prefixes, name prefixes, and middleware. They help keep Laravel applications organized.
A route group combines related routes under a common configuration.
Example:
use Illuminate\Support\Facades\Route;
Route::prefix('admin')->group(function () {
Route::get('/dashboard', function () {
return 'Admin Dashboard';
});
Route::get('/students', function () {
return 'Manage Students';
});
});
The routes become:
/admin/dashboard/admin/students
The prefix() method adds a common URL
prefix to every route inside the group.
Use prefix() when several routes should
start with the same URL segment.
Route::prefix('student')->group(function () {
Route::get('/profile', function () {
return 'Student Profile';
});
Route::get('/courses', function () {
return 'Student Courses';
});
Route::get('/fees', function () {
return 'Student Fees';
});
});
The generated route paths are:
/student/profile/student/courses/student/feesThis is useful for student panels, admin panels, and other sections of a website.
Use name() to give routes a shared
name prefix.
Route::name('admin.')->group(function () {
Route::get('/dashboard', function () {
return 'Admin Dashboard';
})->name('dashboard');
Route::get('/students', function () {
return 'Manage Students';
})->name('students');
});
The route names become:
admin.dashboardadmin.studentsGenerate a URL using the route name:
$url = route('admin.dashboard');
Notice that name('admin.') includes
a trailing dot. Laravel does not add this dot
automatically.
You can combine prefix() and
name() in the same group.
Route::prefix('admin')
->name('admin.')
->group(function () {
Route::get('/dashboard', function () {
return 'Admin Dashboard';
})->name('dashboard');
Route::get('/students', function () {
return 'Manage Students';
})->name('students');
});
Result:
| URL Path | Route Name |
|---|---|
/admin/dashboard |
admin.dashboard |
/admin/students |
admin.students |
URL prefixes affect paths, while name prefixes affect the names used by Laravel's routing helpers.
Middleware can inspect or filter incoming requests before they reach a route.
For example, the built-in auth
middleware generally requires a user to be
authenticated.
Route::middleware('auth')->group(function () {
Route::get('/dashboard', function () {
return 'Protected Dashboard';
});
Route::get('/profile', function () {
return 'Protected Profile';
});
});
Both routes are protected by the
auth middleware.
If the user is not authenticated, Laravel's authentication configuration determines how the request is handled. In a typical browser application, the user may be redirected to a login page.
Adding middleware does not create a login system by itself; authentication must also be configured.
You can protect an entire section of your website and give it a common URL prefix.
Route::prefix('admin')
->middleware('auth')
->group(function () {
Route::get('/dashboard', function () {
return 'Admin Dashboard';
})->name('admin.dashboard');
Route::get('/students', function () {
return 'Manage Students';
})->name('admin.students');
});
These routes use the paths:
/admin/dashboard/admin/studentsBoth routes require authentication. However, authentication alone does not guarantee that a logged-in user is an administrator.
For administrator-only pages, add an appropriate authorization check or admin-specific middleware.
Route groups can direct requests to controller methods instead of using route closures.
Create a controller:
php artisan make:controller AdminController
Add methods inside
app/Http/Controllers/AdminController.php:
public function dashboard()
{
return 'Admin Dashboard';
}
public function students()
{
return 'Manage Students';
}
Define the routes:
use App\Http\Controllers\AdminController;
Route::prefix('admin')
->name('admin.')
->middleware('auth')
->controller(AdminController::class)
->group(function () {
Route::get('/dashboard', 'dashboard')->name('dashboard');
Route::get('/students', 'students')->name('students');
});
The controller group avoids repeating the controller class for every route. The methods must exist in the controller.
Route groups can be nested to organize larger applications.
Route::prefix('admin')->name('admin.')->group(function () {
Route::prefix('students')->name('students.')->group(function () {
Route::get('/', function () {
return 'All Students';
})->name('index');
Route::get('/create', function () {
return 'Create Student';
})->name('create');
});
});
The resulting route names are:
admin.students.indexadmin.students.create
Their URL paths are /admin/students
and /admin/students/create.
Inspect registered routes:
php artisan route:list
You can inspect the registered URLs, route names, and middleware in the command output.
Create the following route groups in
routes/web.php.
admin.admin..admin.dashboard.auth middleware.controller().php artisan route:list and inspect your routes.Remember: a URL prefix organizes paths, a name prefix organizes route names, and middleware controls how requests are handled.
1. Which method adds a URL prefix to a route group?
2. Which method applies middleware to a route group?
3. What does name('admin.') do in a route group?