Controller middleware in Laravel is a way to filter HTTP requests entering your application by running code before or after a controller action. It helps manage tasks like authentication, logging, or input validation.
Click to reveal answer
beginner
How do you apply middleware to a Laravel controller?
You apply middleware in a controller by calling the $this->middleware('middlewareName') method inside the controller's constructor. This runs the middleware on all or selected controller methods.
Click to reveal answer
intermediate
How can you restrict middleware to only certain methods in a controller?
Use the only or except options when applying middleware, like $this->middleware('auth')->only(['index', 'show']) to run middleware only on those methods.
Click to reveal answer
beginner
What happens if middleware denies a request in Laravel?
If middleware denies a request, it usually returns a response like a redirect or error before the controller method runs, stopping further processing.
Click to reveal answer
intermediate
Can you apply multiple middleware to a single controller in Laravel?
Yes, you can apply multiple middleware by passing an array to $this->middleware(['auth', 'verified']). They run in the order listed.
Click to reveal answer
How do you add middleware to a Laravel controller?
ABy adding middleware in the model class
BInside the controller constructor using $this->middleware()
CBy editing the routes/web.php file only
DBy calling middleware in the view file
✗ Incorrect
Middleware is added inside the controller constructor using $this->middleware('name').
Which method restricts middleware to specific controller methods?
Aboth only() and except()
Bexcept()
Conly()
Dmiddleware()
✗ Incorrect
You can use both only() to include and except() to exclude methods when applying middleware.
What does middleware do if it blocks a request?
AReturns a response before controller runs
BRuns the controller anyway
CDeletes the request
DLogs the request and continues
✗ Incorrect
Middleware returns a response like redirect or error before the controller method runs.
Can you apply multiple middleware to one controller?
AOnly in the route file
BNo, only one middleware per controller
COnly if middleware are chained in routes
DYes, by passing an array to $this->middleware()
✗ Incorrect
Multiple middleware can be applied by passing an array to $this->middleware(['one', 'two']).
Where is controller middleware typically defined?
AIn the CSS file
BIn the blade view
CIn the controller constructor
DIn the database migration
✗ Incorrect
Middleware is defined inside the controller constructor using $this->middleware().
Explain how to apply middleware to only some methods of a Laravel controller.
Think about how to include or exclude methods when calling $this->middleware.
You got /4 concepts.
Describe what happens when middleware denies a request in Laravel.
Middleware can stop the request before it reaches the controller.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of middleware in a Laravel controller?
easy
A. To connect the controller to the database
B. To define routes for the controller
C. To run code before or after controller methods for access control or modification
D. To create views for the controller
Solution
Step 1: Understand middleware role
Middleware acts as a filter that runs before or after controller methods to control access or modify requests/responses.
Step 2: Differentiate from other controller tasks
Defining routes, connecting to databases, or creating views are not middleware responsibilities.
Final Answer:
To run code before or after controller methods for access control or modification -> Option C
Quick Check:
Middleware controls access and actions around controller methods = A [OK]
Thinking multiple middleware calls override each other
Assuming except() needs array only
Believing syntax causes error
5. You want to apply the 'verified' middleware to all controller methods except 'index' and 'show', and also apply 'log' middleware only to 'store' and 'update'. Which constructor code correctly implements this using arrays?
hard
A. public function __construct() {
$this->middleware('verified')->only('index', 'show');
$this->middleware('log')->except('store', 'update');
}
B. public function __construct() {
$this->middleware('verified')->except(['index', 'show']);
$this->middleware('log')->only(['store', 'update']);
}
C. public function __construct() {
$this->middleware('verified');
$this->middleware('log');
}
D. public function __construct() {
$this->middleware('verified')->except('index', 'show');
$this->middleware('log')->only('store', 'update');
}
Solution
Step 1: Apply 'verified' middleware except on 'index' and 'show'
Use except() with array to exclude 'index' and 'show' from 'verified' middleware.
Step 2: Apply 'log' middleware only on 'store' and 'update'
Use only() with array to limit 'log' middleware to 'store' and 'update' methods.
Step 3: Compare options
public function __construct() {
$this->middleware('verified')->except('index', 'show');
$this->middleware('log')->only('store', 'update');
} uses except() and only() with multiple string arguments (valid but less common). public function __construct() {
$this->middleware('verified')->except(['index', 'show']);
$this->middleware('log')->only(['store', 'update']);
} uses arrays which is clearer and recommended. Options B and C do not match requirements.
Final Answer:
public function __construct() {
$this->middleware('verified')->except(['index', 'show']);
$this->middleware('log')->only(['store', 'update']);
} -> Option B
Quick Check:
Use except() and only() with arrays for clarity = A [OK]
Hint: Use except() and only() with arrays for multiple methods [OK]
Common Mistakes:
Mixing only() and except() incorrectly
Not using arrays for multiple methods
Applying middleware to wrong methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.