Bird
0
0

Which is the correct way to add middleware to a Laravel controller constructor?

easy📝 Syntax Q12 of 15
Laravel - Controllers
Which is the correct way to add middleware to a Laravel controller constructor?
A$this->middleware('auth');
Bmiddleware->add('auth');
CaddMiddleware('auth');
DuseMiddleware('auth');
Step-by-Step Solution
Solution:
  1. Step 1: Recall Laravel middleware syntax

    In Laravel controllers, middleware is added inside the constructor using $this->middleware('name');.
  2. Step 2: Check other options for syntax correctness

    Options A, C, and D do not follow Laravel's middleware syntax and will cause errors.
  3. Final Answer:

    $this->middleware('auth'); -> Option A
  4. Quick Check:

    Use $this->middleware() in constructor = B [OK]
Quick Trick: Use $this->middleware('name') inside constructor [OK]
Common Mistakes:
  • Using incorrect method names
  • Calling middleware outside constructor
  • Missing $this keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes