Laravel Route Groups
A route group is a set of routes that share a setting. The group keeps related routes together and applies the shared setting to each one.
One shared start
This group gives the dashboard the address /l10/admin/dashboard.
Route::prefix('l10/admin')->group(function () {
Route::get('/dashboard', fn () => 'Admin dashboard');
});A second page in the group
This group holds two admin pages.
Route::prefix('l10/admin')->group(function () {
Route::get('/dashboard', fn () => 'Admin dashboard');
Route::get('/users', fn () => 'Admin users');
});Another group
This group gives the status page the address /l10/api/status.
Route::prefix('l10/api')->group(function () {
Route::get('/status', fn () => 'API is up');
});Example
<?php
use Illuminate\Support\Facades\Route;
Route::prefix('l10/admin')->group(function () {
Route::get('/dashboard', fn () => 'Admin dashboard');
Route::get('/users', fn () => 'Admin users');
});Key points
- Route group: a set of routes that share a setting.
- prefix() adds a shared start to every address in the group.
- group() holds the routes that share the setting.
- A group keeps related routes together in one place.
- Use a group when several routes share the same start.
In this example, the first lines load the Route helper. The next lines open a group with the shared start l10/admin. A visit to /l10/admin/dashboard sees Admin dashboard.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does prefix() add to every route in a group?