Terminable middleware lets you run code after the response is sent to the user. This helps with tasks like logging or cleanup without slowing down the user.
Terminable middleware in Laravel
class ExampleMiddleware implements TerminableMiddlewareInterface { public function handle($request, Closure $next) { // Code before response $response = $next($request); // Code after response preparation but before sending return $response; } public function terminate($request, $response) { // Code that runs after response is sent } }
The terminate method runs after the response is sent to the browser.
Make sure your middleware is registered in the kernel.php to use terminable middleware.
class LogAfterResponse implements TerminableMiddlewareInterface { public function handle($request, Closure $next) { return $next($request); } public function terminate($request, $response) { // Log request info after response Log::info('Request completed: ' . $request->path()); } }
class CleanupAfterResponse implements TerminableMiddlewareInterface { public function handle($request, Closure $next) { return $next($request); } public function terminate($request, $response) { // Clear temporary files or cache Storage::delete('temp-file.txt'); } }
This middleware logs the URL path the user visited after the response is sent. It does not delay the page loading.
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Contracts\Middleware\TerminableMiddleware; use Illuminate\Support\Facades\Log; class AfterResponseLogger implements TerminableMiddleware { public function handle($request, Closure $next) { // Just pass the request to next middleware or controller return $next($request); } public function terminate($request, $response) { // Runs after response sent Log::info('User visited: ' . $request->path()); } } // In Kernel.php, register this middleware in $middleware array or route middleware // When a user visits a page, the response is sent immediately. // After that, the terminate method logs the path.
Terminable middleware only works if the middleware implements the terminate method.
It is useful for tasks that don't need to block the user waiting for the response.
Remember to register the middleware properly in app/Http/Kernel.php.
Terminable middleware runs code after the response is sent to the user.
Use it for logging, cleanup, or background tasks.
Implement the terminate method to use this feature.
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
