Bird
0
0

Examine this controller constructor:

medium📝 Debug Q6 of 15
Laravel - Controllers
Examine this controller constructor:
public function __construct() {
    $this->middleware('auth')->only('index');
    $this->middleware('guest')->only('index');
}

What is the issue with this middleware assignment?
AMiddleware names must be passed as arrays, not strings
BMiddleware cannot be assigned using the only() method
CBoth 'auth' and 'guest' middleware are applied to the same method, causing a conflict
DMiddleware cannot be assigned in the constructor
Step-by-Step Solution
Solution:
  1. Step 1: Analyze Middleware Assignments

    Both 'auth' and 'guest' middleware are applied exclusively to the 'index' method.
  2. Step 2: Understand Middleware Purpose

    'auth' requires the user to be authenticated, while 'guest' requires the user to be unauthenticated. Applying both to the same method causes a logical conflict.
  3. Final Answer:

    Both 'auth' and 'guest' middleware are applied to the same method, causing a conflict -> Option C
  4. Quick Check:

    Conflicting middleware on same method [OK]
Quick Trick: Don't assign conflicting middleware to the same method [OK]
Common Mistakes:
  • Assuming only() cannot be used
  • Thinking middleware names must be arrays
  • Believing middleware can't be assigned in constructors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes