0
0
Laravelframework~10 mins

Route groups in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route groups
Define Route Group
Apply Shared Attributes
Define Routes Inside Group
Routes Inherit Group Attributes
Routes Registered in Router
Route groups let you bundle routes sharing common settings like middleware or prefixes, so all routes inside inherit those attributes.
Execution Sample
Laravel
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () {
    Route::get('dashboard', fn() => 'Admin Dashboard');
    Route::get('profile', fn() => 'Admin Profile');
});
This code creates a route group with 'admin' prefix and 'auth' middleware, applying them to two routes inside.
Execution Table
StepActionGroup AttributesRoute DefinedRoute Full PathMiddleware Applied
1Start group definition{prefix: 'admin', middleware: 'auth'}NoneNoneNone
2Define route 'dashboard'{prefix: 'admin', middleware: 'auth'}dashboard/admin/dashboardauth
3Define route 'profile'{prefix: 'admin', middleware: 'auth'}profile/admin/profileauth
4End group definitionNoneNoneNoneNone
💡 All routes inside group inherit prefix and middleware; group ends after last route.
Variable Tracker
VariableStartAfter 1After 2Final
groupAttributes{}{prefix: 'admin', middleware: 'auth'}{prefix: 'admin', middleware: 'auth'}None
routePathNonedashboardprofileNone
fullRoutePathNone/admin/dashboard/admin/profileNone
middlewareNoneauthauthNone
Key Moments - 2 Insights
Why do routes inside the group automatically get the '/admin' prefix?
Because the group defines a 'prefix' attribute applied to all routes inside, as shown in execution_table rows 2 and 3 where fullRoutePath includes '/admin/'.
Does the middleware 'auth' apply to each route individually or only once for the group?
Middleware applies to each route individually but inherited from the group, as seen in execution_table rows 2 and 3 where middleware is 'auth' for both routes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the full path of the 'profile' route?
A/profile
B/admin/profile
C/dashboard/profile
D/auth/profile
💡 Hint
Check the 'Route Full Path' column at step 3 in the execution_table.
At which step does the middleware 'auth' get applied to routes?
AStep 2 and 3
BStep 1
CStep 4
DMiddleware is not applied
💡 Hint
Look at the 'Middleware Applied' column for steps 2 and 3 in the execution_table.
If we remove the 'prefix' from groupAttributes, what changes in the execution_table?
ARoutes will not be defined
BMiddleware will be removed
CRoute Full Path will not include '/admin/' prefix
DNo change at all
💡 Hint
Refer to the 'Group Attributes' and 'Route Full Path' columns in execution_table rows 2 and 3.
Concept Snapshot
Route groups bundle routes sharing settings.
Use Route::group with attributes like prefix, middleware.
All routes inside inherit these attributes.
Simplifies route management and avoids repetition.
Routes inside group get full path with prefix.
Middleware applies to each grouped route.
Full Transcript
Route groups in Laravel let you group routes that share common settings like URL prefixes or middleware. You define a group with Route::group and pass attributes like 'prefix' and 'middleware'. Then, inside the group, you define routes normally. Each route automatically gets the group's prefix added to its path and the middleware applied. This helps keep your routes organized and avoids repeating the same settings for each route. For example, a group with prefix 'admin' and middleware 'auth' will make routes like 'dashboard' become '/admin/dashboard' and require authentication. The execution table shows how the group attributes apply step-by-step to each route defined inside the group.