Middleware helps control what happens before or after a request reaches your controller. It acts like a gatekeeper to add checks or actions easily.
Controller middleware in Laravel
public function __construct()
{
$this->middleware('middlewareName');
}
// Or apply middleware to specific methods
$this->middleware('middlewareName')->only(['methodName']);
$this->middleware('middlewareName')->except(['methodName']);Middleware is added inside the controller's constructor method.
You can apply middleware to all methods or only some methods using only or except.
public function __construct()
{
$this->middleware('auth');
}public function __construct()
{
$this->middleware('auth')->only(['edit', 'update']);
}public function __construct()
{
$this->middleware('auth')->except(['index', 'show']);
}This controller uses the 'auth' middleware only on the 'create' and 'store' methods. So, users must be logged in to create or save posts. The 'index' method is open to everyone.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PostController extends Controller { public function __construct() { $this->middleware('auth')->only(['create', 'store']); } public function index() { return 'Showing all posts'; } public function create() { return 'Show form to create a post'; } public function store(Request $request) { return 'Saving new post'; } }
Middleware names like 'auth' refer to middleware registered in your app's HTTP kernel.
You can create custom middleware for your own checks and use them the same way.
Middleware runs before the controller method and can stop the request or modify it.
Middleware controls access and actions before or after controller methods run.
Add middleware in the controller constructor using $this->middleware().
Use only or except to limit middleware to specific methods.
Practice
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 CQuick Check:
Middleware controls access and actions around controller methods = A [OK]
- Confusing middleware with routing
- Thinking middleware creates views
- Assuming middleware connects to database
Solution
Step 1: Recall Laravel middleware syntax
In Laravel controllers, middleware is added inside the constructor using$this->middleware('name');.Step 2: Check other options for syntax correctness
Options A, C, and D do not follow Laravel's middleware syntax and will cause errors.Final Answer:
$this->middleware('auth'); -> Option AQuick Check:
Use $this->middleware() in constructor = B [OK]
- Using incorrect method names
- Calling middleware outside constructor
- Missing $this keyword
public function __construct() {
$this->middleware('auth')->only('edit', 'update');
}Which methods will have the 'auth' middleware applied?
Solution
Step 1: Understand the use of only() method
Theonly('edit', 'update')limits middleware to run only on these two methods.Step 2: Confirm middleware application
Middleware 'auth' applies only to 'edit' and 'update', other methods are unaffected.Final Answer:
Only the edit and update methods -> Option AQuick Check:
only() limits middleware to specified methods = D [OK]
- Confusing only() with except()
- Assuming middleware applies to all methods
- Thinking syntax causes error
public function __construct() {
$this->middleware('auth')->except('index', 'show');
$this->middleware('log');
}Solution
Step 1: Check multiple middleware calls
Laravel allows multiple middleware calls in constructor; they stack and apply in order.Step 2: Validate except() usage
except() accepts multiple string arguments or an array; both are valid.Final Answer:
No error; both middleware apply correctly -> Option DQuick Check:
Multiple middleware calls and except() usage are valid = C [OK]
- Thinking multiple middleware calls override each other
- Assuming except() needs array only
- Believing syntax causes error
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 BQuick Check:
Use except() and only() with arrays for clarity = A [OK]
- Mixing only() and except() incorrectly
- Not using arrays for multiple methods
- Applying middleware to wrong methods
Ready to prove it?
Pass the test and this pattern moves unknown
