Performance: Controller middleware
Controller middleware affects the server response time and can indirectly impact the time until the page starts rendering by adding processing before the controller action.
public function __construct() {
$this->middleware('auth'); // Use built-in lightweight middleware
// Move heavy logic inside controller or queue
}public function __construct() {
$this->middleware(function ($request, $next) {
// Heavy database queries or complex logic here
$data = DB::table('large_table')->get();
// Some complex processing
return $next($request);
});
}| Pattern | Server Processing | Response Delay | Impact on LCP | Verdict |
|---|---|---|---|---|
| Heavy logic in middleware | High CPU and DB queries | Adds 100+ ms delay | Worsens LCP significantly | [X] Bad |
| Lightweight middleware (e.g., auth) | Minimal CPU | Minimal delay | Improves LCP | [OK] Good |
$this->middleware('name');.public function __construct() {
$this->middleware('auth')->only('edit', 'update');
}only('edit', 'update') limits middleware to run only on these two methods.public function __construct() {
$this->middleware('auth')->except('index', 'show');
$this->middleware('log');
}Pass the test and this pattern moves unknown