Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a middleware that accepts a parameter.
Laravel
public function handle($request, Closure $next, [1]) { // Middleware logic here return $next($request); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $request or $next as parameter names after $next.
Not including the parameter in the method signature.
✗ Incorrect
The middleware handle method accepts parameters after $next. The parameter name can be anything, here $param is used.
2fill in blank
mediumComplete the route middleware assignment to pass a parameter named 'role'.
Laravel
Route::get('/dashboard', function () { // Controller logic })->middleware('role:[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add the colon before the parameter.
Passing the parameter as a separate argument instead of in the string.
✗ Incorrect
To pass a parameter to middleware, append it after a colon. Here 'admin' is passed as the role parameter.
3fill in blank
hardFix the error in the middleware handle method to correctly accept multiple parameters.
Laravel
public function handle($request, Closure $next, [1]) { // Logic using $role and $level return $next($request); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using spaces or semicolons instead of commas.
Not including all parameters in the method signature.
✗ Incorrect
Multiple parameters must be separated by commas in the method signature.
4fill in blank
hardFill both blanks to correctly retrieve middleware parameters inside the handle method.
Laravel
public function handle($request, Closure $next, [1], [2]) { if ($role === 'admin' && $level > 5) { return $next($request); } return redirect('home'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names that don't match the code logic.
Using generic names like $param when specific names are used.
✗ Incorrect
The middleware parameters are named $role and $level to match usage inside the method.
5fill in blank
hardFill all three blanks to create a middleware that checks a user's role and status parameters.
Laravel
public function handle($request, Closure $next, [1], [2], [3]) { if ($role === 'editor' && $status === 'active' && $level >= 3) { return $next($request); } return redirect('login'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect or mismatched parameter names.
Not including all parameters in the method signature.
✗ Incorrect
The middleware parameters must be named $role, $status, and $level to match the conditions inside the method.