Bird
Raised Fist0
Laravelframework~20 mins

Middleware parameters in Laravel - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the correct way to pass parameters to a middleware in Laravel's route definition?
easy
A. Pass parameters inside parentheses after the middleware name, like auth(admin, active)
B. Add parameters as query strings after the middleware name, like auth?role=admin&status=active
C. Use a colon after the middleware name, then list parameters separated by commas, like auth:admin,active
D. List parameters separated by spaces after the middleware name, like auth admin active

Solution

  1. Step 1: Understand Laravel middleware parameter syntax

    Laravel middleware parameters are passed after the middleware name using a colon, then parameters separated by commas.
  2. Step 2: Identify correct syntax in route definition

    The correct syntax is middleware('auth:admin,active'), matching Use a colon after the middleware name, then list parameters separated by commas, like auth:admin,active.
  3. Final Answer:

    Use a colon after the middleware name, then list parameters separated by commas, like auth:admin,active -> Option C
  4. Quick Check:

    Middleware parameters = colon + comma-separated [OK]
Hint: Remember: colon then commas separate middleware parameters [OK]
Common Mistakes:
  • Using parentheses instead of colon and commas
  • Passing parameters as query strings
  • Separating parameters by spaces
2. Which of the following is the correct syntax to access middleware parameters inside the handle method?
easy
A. Use $request->params() to get parameters
B. Use the third argument of handle method as ...$params
C. Use $next($request, $params) to pass parameters
D. Use $this->params property inside middleware

Solution

  1. Step 1: Recall middleware handle method signature

    Laravel middleware handle method accepts $request, $next, and optional parameters as variadic arguments.
  2. Step 2: Identify correct way to receive parameters

    Parameters are received as additional arguments after $next, typically using ...$params.
  3. Final Answer:

    Use the third argument of handle method as ...$params -> Option B
  4. Quick Check:

    Middleware parameters = variadic arguments in handle [OK]
Hint: Use ...$params after $next in handle method [OK]
Common Mistakes:
  • Trying to get parameters from $request object
  • Passing parameters to $next instead of receiving them
  • Using non-existent $this->params property
3. Given this middleware handle method:
public function handle($request, Closure $next, $role)
{
    if ($request->user()->role !== $role) {
        return redirect('home');
    }
    return $next($request);
}

And route middleware usage: middleware('checkRole:admin')
What happens if a user with role 'editor' accesses the route?
medium
A. The user is redirected to 'home' because roles don't match
B. The user proceeds to the route normally
C. A runtime error occurs due to missing parameters
D. The middleware ignores the role and allows access

Solution

  1. Step 1: Understand middleware logic with parameter

    The middleware compares the user's role with the passed parameter $role. If they differ, it redirects to 'home'.
  2. Step 2: Apply given user role and parameter

    User role is 'editor', parameter is 'admin', so roles don't match, triggering redirect.
  3. Final Answer:

    The user is redirected to 'home' because roles don't match -> Option A
  4. Quick Check:

    User role != parameter role = redirect [OK]
Hint: Mismatch role parameter causes redirect in middleware [OK]
Common Mistakes:
  • Assuming user proceeds despite role mismatch
  • Expecting runtime error due to parameters
  • Thinking middleware ignores parameters
4. Consider this middleware handle method:
public function handle($request, Closure $next, $status)
{
    if ($status !== 'active') {
        return redirect('inactive');
    }
    return $next($request);
}

And route middleware usage: middleware('checkStatus')
What error or behavior occurs?
medium
A. The middleware ignores the parameter and throws a syntax error
B. The middleware runs normally and allows access
C. The middleware redirects to 'inactive' always
D. A runtime error because the required parameter $status is missing

Solution

  1. Step 1: Check middleware parameter requirement

    The handle method requires a parameter $status, but the middleware is used without parameters.
  2. Step 2: Understand Laravel behavior on missing parameters

    Laravel throws a runtime error if a required middleware parameter is missing when called.
  3. Final Answer:

    A runtime error because the required parameter $status is missing -> Option D
  4. Quick Check:

    Missing middleware parameter = runtime error [OK]
Hint: Middleware parameters must be passed or error occurs [OK]
Common Mistakes:
  • Assuming middleware runs without parameters
  • Expecting redirect without parameter
  • Confusing syntax error with runtime error
5. You want to create a middleware that accepts multiple roles as parameters and allows access if the user has any of those roles. Which of the following handle method implementations correctly uses middleware parameters to achieve this?
hard
A.
public function handle($request, Closure $next, ...$roles)
{
    if (!in_array($request->user()->role, $roles)) {
        return redirect('home');
    }
    return $next($request);
}
B.
public function handle($request, Closure $next, $roles)
{
    if ($request->user()->role !== $roles) {
        return redirect('home');
    }
    return $next($request);
}
C.
public function handle($request, Closure $next, $roles)
{
    foreach ($roles as $role) {
        if ($request->user()->role === $role) {
            return $next($request);
        }
    }
    return redirect('home');
}
D.
public function handle($request, Closure $next, ...$roles)
{
    if ($request->user()->role === $roles) {
        return $next($request);
    }
    return redirect('home');
}

Solution

  1. Step 1: Understand variadic parameters usage

    Using ...$roles collects all passed roles into an array for easy checking.
  2. Step 2: Check role membership logic

    Using in_array checks if user's role is in the roles array, allowing access if true.
  3. Step 3: Evaluate other options

    public function handle($request, Closure $next, $roles)
    {
        if ($request->user()->role !== $roles) {
            return redirect('home');
        }
        return $next($request);
    }
    treats roles as single string, not multiple; C expects $roles as array but parameters are strings; D compares string to array directly, which fails.
  4. Final Answer:

    Use ...$roles and in_array($request->user()->role, $roles) to check multiple roles -> Option A
  5. Quick Check:

    Variadic params + in_array = multiple roles check [OK]
Hint: Use ...$params and in_array() to check multiple middleware roles [OK]
Common Mistakes:
  • Treating multiple parameters as single string
  • Comparing string directly to array
  • Not using variadic parameters for multiple roles
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