Discover how to speed up your app by running tasks after the user sees the page!
Why Terminable middleware in Laravel? - Purpose & Use Cases
Imagine you want to log user activity or clean up resources right after your web page finishes loading for the user.
You try to do this inside your main request handling code, but it slows down the response or makes your code messy.
Doing post-response tasks manually means you either delay sending the page to the user or mix unrelated code together.
This makes your app slower and harder to maintain.
Terminable middleware lets you run code after the response is sent to the user, keeping your app fast and your code clean.
handle request send response then run cleanup code (delays response)
handle request send response terminable middleware runs cleanup code after response
You can perform tasks like logging, cache clearing, or notifications without slowing down the user's experience.
After a user submits a form, you want to log their action and send a notification email, but only after the page loads quickly for them.
Terminable middleware runs code after response is sent.
Keeps user experience fast and smooth.
Makes post-response tasks easy and organized.
Practice
Solution
Step 1: Understand middleware timing
Terminable middleware runs after the response is sent, unlike regular middleware that runs before.Step 2: Identify terminable middleware purpose
It is used for tasks like logging or cleanup after response delivery.Final Answer:
To run code after the HTTP response has been sent to the user -> Option DQuick Check:
Terminable middleware = post-response code [OK]
- Confusing terminable middleware with regular middleware
- Thinking it runs before the controller
- Assuming it handles request validation
Solution
Step 1: Recall terminable middleware interface
Terminable middleware requires aterminatemethod to run code after response.Step 2: Differentiate from handle method
handleruns before response,terminateruns after.Final Answer:
terminate -> Option CQuick Check:
Post-response method = terminate [OK]
- Using handle instead of terminate for post-response code
- Assuming process or finish are valid methods
- Not implementing terminate method at all
class LogAfterResponse {
public function handle($request, Closure $next) {
return $next($request);
}
public function terminate($request, $response) {
Log::info('Response sent with status: ' . $response->status());
}
}Solution
Step 1: Analyze handle method
The handle method passes the request to next middleware/controller without changes.Step 2: Analyze terminate method
After response is sent, terminate logs the response status code usingLog::info.Final Answer:
Logs the response status code after sending the response -> Option BQuick Check:
terminate logs status post-response [OK]
- Thinking terminate runs before response
- Assuming terminate is not called automatically
- Confusing request and response parameters
class CleanupMiddleware {
public function handle($request, Closure $next) {
return $next($request);
}
public function terminate() {
// cleanup code here
}
}Solution
Step 1: Check terminate method signature
Terminable middleware's terminate method must accept both$requestand$responseparameters.Step 2: Identify missing parameters
The code's terminate method has no parameters, which will cause errors.Final Answer:
terminate method must accept $request and $response parameters -> Option AQuick Check:
terminate signature requires request and response [OK]
- Omitting parameters in terminate method
- Renaming terminate method incorrectly
- Misunderstanding handle method return type
class RequestTimer {
protected $startTime;
public function handle($request, Closure $next) {
$this->startTime = microtime(true);
return $next($request);
}
public function terminate($request, $response) {
// What code should go here?
}
}Solution
Step 1: Calculate duration in terminate method
Use current time minus stored start time to get total duration.Step 2: Log the duration
UseLog::infoto record the time taken after response is sent.Final Answer:
$duration = microtime(true) - $this->startTime; \Log::info('Request took ' . $duration . ' seconds'); -> Option AQuick Check:
Duration = now - startTime, then log [OK]
- Adding times instead of subtracting
- Returning values from terminate method
- Calling handle method inside terminate
Ready to prove it?
Pass the test and this pattern moves unknown
