0
0
Laravelframework~10 mins

Middleware parameters in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A$next
B$request
C$param
D$middleware
Attempts:
3 left
💡 Hint
Common Mistakes
Using $request or $next as parameter names after $next.
Not including the parameter in the method signature.
2fill in blank
medium

Complete 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'
Aadmin
Buser
Cguest
Dmanager
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.
3fill in blank
hard

Fix 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'
A$role, $level
B$role $level
C$role; $level
D$role & $level
Attempts:
3 left
💡 Hint
Common Mistakes
Using spaces or semicolons instead of commas.
Not including all parameters in the method signature.
4fill in blank
hard

Fill 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'
A$role
B$level
C$param
D$user
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.
5fill in blank
hard

Fill 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'
A$role
B$status
C$level
D$user
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect or mismatched parameter names.
Not including all parameters in the method signature.