0
0
Laravelframework~20 mins

Middleware parameters in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Laravel middleware receive parameters?

Consider a Laravel middleware that accepts parameters. How are these parameters passed when defining middleware in routes?

Laravel
Route::get('/profile', function () {
    // ...
})->middleware('role:admin,editor');
AParameters are passed as an array in the middleware method call.
BParameters are passed as a comma-separated string after a colon in the middleware name.
CParameters are passed as query parameters in the URL.
DParameters are passed via HTTP headers automatically.
Attempts:
2 left
💡 Hint

Think about how the middleware name is written in the route definition.

state_output
intermediate
2:00remaining
What is the output of middleware parameter parsing?

Given this middleware handle method, what will be the value of $roles when the middleware is called with role:admin,editor?

Laravel
public function handle($request, Closure $next, ...$roles)
{
    return response()->json($roles);
}
A[]
B["role:admin", "editor"]
C["admin,editor"]
D["admin", "editor"]
Attempts:
2 left
💡 Hint

Look at how the parameters are captured using the spread operator ...$roles.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in middleware parameter usage

Which option contains a syntax error when defining middleware with parameters in Laravel routes?

Laravel
Route::get('/dashboard', function () {
    // ...
})->middleware('auth', 'role:admin');
A->middleware('auth', 'role:admin')
B->middleware('role:admin')
C->middleware(['auth', 'role:admin'])
D->middleware('auth')->middleware('role:admin')
Attempts:
2 left
💡 Hint

Check the method signature of middleware() in Laravel routing.

🔧 Debug
advanced
2:00remaining
Why does this middleware parameter not work as expected?

Given this middleware handle method, why does passing role:admin,editor not restrict access properly?

Laravel
public function handle($request, Closure $next, $role)
{
    if (! $request->user()->hasRole($role)) {
        abort(403);
    }
    return $next($request);
}
AOnly the first parameter 'admin' is captured; 'editor' is ignored because $role is a single variable.
BThe middleware does not receive any parameters because of missing type hint.
CThe abort(403) call is misplaced and never executes.
DThe handle method should return a boolean, not a response.
Attempts:
2 left
💡 Hint

Consider how multiple parameters are captured in the method signature.

🧠 Conceptual
expert
3:00remaining
How to access middleware parameters in a Laravel middleware class?

Which code snippet correctly accesses multiple middleware parameters passed as role:admin,editor in a Laravel middleware class?

Apublic function handle($request, Closure $next, $role1, $role2) { /* use $role1 and $role2 */ }
Bpublic function handle($request, Closure $next, $roles) { $roles = explode(',', $roles); }
Cpublic function handle($request, Closure $next, ...$roles) { /* use $roles array */ }
Dpublic function handle($request, Closure $next) { $roles = func_get_args(); }
Attempts:
2 left
💡 Hint

Think about how PHP collects variable-length argument lists.