Bird
Raised Fist0
Laravelframework~10 mins

Route groups in Laravel - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using Route::group() in Laravel?
easy
A. To register routes only for API requests
B. To define a single route with multiple HTTP methods
C. To create a route that redirects to another URL
D. To apply shared settings like middleware or prefixes to multiple routes at once

Solution

  1. Step 1: Understand Route Group Purpose

    Route groups allow you to bundle routes that share common attributes like middleware or URL prefixes.
  2. Step 2: Compare with Other Options

    Options A, B, and C describe different routing features, not the grouping concept.
  3. Final Answer:

    To apply shared settings like middleware or prefixes to multiple routes at once -> Option D
  4. Quick Check:

    Route groups = shared settings [OK]
Hint: Route groups bundle routes with common settings fast [OK]
Common Mistakes:
  • Thinking route groups define single routes
  • Confusing route groups with route redirects
  • Assuming route groups are only for API routes
2. Which of the following is the correct syntax to create a route group with a prefix admin in Laravel?
easy
A. Route::group('prefix' => 'admin', function () { /* routes */ });
B. Route::group(['prefix' => 'admin'], function () { /* routes */ });
C. Route::group(['admin' => 'prefix'], function () { /* routes */ });
D. Route::group(function () { prefix('admin'); /* routes */ });

Solution

  1. Step 1: Recall Route Group Syntax

    Route groups use an array of attributes as the first argument, e.g., ['prefix' => 'admin'].
  2. Step 2: Identify Correct Array Syntax

    Route::group(['prefix' => 'admin'], function () { /* routes */ }); correctly uses the array with 'prefix' key and a closure function.
  3. Final Answer:

    Route::group(['prefix' => 'admin'], function () { /* routes */ }); -> Option B
  4. Quick Check:

    Array with 'prefix' key = correct syntax [OK]
Hint: Use array with 'prefix' key inside Route::group [OK]
Common Mistakes:
  • Missing array brackets around attributes
  • Swapping key and value in array
  • Trying to call prefix() inside the closure
3. Given the following route group code, what URL will respond to the Route::get('dashboard', ...) route?
Route::group(['prefix' => 'admin'], function () {
    Route::get('dashboard', function () {
        return 'Admin Dashboard';
    });
});
medium
A. /admin/dashboard
B. /admin
C. /dashboard
D. /dashboard/admin

Solution

  1. Step 1: Understand Prefix Effect

    The 'prefix' => 'admin' adds '/admin' before all routes inside the group.
  2. Step 2: Combine Prefix and Route URI

    The route URI is 'dashboard', so combined URL is '/admin/dashboard'.
  3. Final Answer:

    /admin/dashboard -> Option A
  4. Quick Check:

    Prefix + route URI = /admin/dashboard [OK]
Hint: Prefix adds before route URI in URL [OK]
Common Mistakes:
  • Ignoring the prefix and using only route URI
  • Appending prefix after route URI
  • Confusing route name with URL
4. Identify the error in this route group code:
Route::group(['middleware' => 'auth'], function {
    Route::get('profile', function () {
        return 'User Profile';
    });
});
medium
A. Missing parentheses after function keyword
B. Middleware should be an array, not a string
C. Route::group cannot use middleware
D. Route URI 'profile' is invalid

Solution

  1. Step 1: Check Closure Syntax

    The anonymous function must have parentheses: function() { ... }.
  2. Step 2: Validate Middleware Usage

    Middleware can be a string or array, so that is correct here.
  3. Final Answer:

    Missing parentheses after function keyword -> Option A
  4. Quick Check:

    Anonymous function needs () after function [OK]
Hint: Always add () after function keyword for closures [OK]
Common Mistakes:
  • Omitting parentheses in anonymous functions
  • Thinking middleware must be array always
  • Assuming route URI 'profile' is invalid
5. You want to create a route group with prefix api and middleware auth:api. Inside it, you define a route GET /users that returns a JSON list of users. Which is the correct way to write this in Laravel?
hard
A. Route::middleware('auth:api')->group(['prefix' => 'api'], function () { Route::get('users', function () { return response()->json(['users' => []]); }); });
B. Route::group(['prefix' => 'api'], function () { Route::middleware('auth:api')->get('users', function () { return ['users' => []]; }); });
C. Route::group(['prefix' => 'api', 'middleware' => 'auth:api'], function () { Route::get('users', function () { return response()->json(['users' => []]); }); });
D. Route::group(['middleware' => 'auth:api'], function () { Route::prefix('api')->get('users', function () { return response()->json(['users' => []]); }); });

Solution

  1. Step 1: Combine Prefix and Middleware in One Group

    Route::group(['prefix' => 'api', 'middleware' => 'auth:api'], function () { Route::get('users', function () { return response()->json(['users' => []]); }); }); correctly passes both 'prefix' and 'middleware' as keys in the attributes array.
  2. Step 2: Check Route Definition Inside Group

    The route uses Route::get with URI 'users' and returns JSON response properly.
  3. Step 3: Verify Other Options

    Route::group(['prefix' => 'api'], function () { Route::middleware('auth:api')->get('users', function () { return ['users' => []]; }); }); applies middleware only to the route, not the group prefix. Route::middleware('auth:api')->group(['prefix' => 'api'], function () { Route::get('users', function () { return response()->json(['users' => []]); }); }); has wrong group syntax. Route::group(['middleware' => 'auth:api'], function () { Route::prefix('api')->get('users', function () { return response()->json(['users' => []]); }); }); misuses prefix inside the route.
  4. Final Answer:

    Route::group(['prefix' => 'api', 'middleware' => 'auth:api'], function () { Route::get('users', function () { return response()->json(['users' => []]); }); }); -> Option C
  5. Quick Check:

    Group attributes array with prefix and middleware [OK]
Hint: Put prefix and middleware together in group attributes array [OK]
Common Mistakes:
  • Splitting prefix and middleware into separate calls incorrectly
  • Misplacing prefix() inside route closure
  • Using middleware() outside group incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard