Terminable middleware is middleware that runs after the HTTP response has been sent to the browser. It allows you to perform tasks like logging or cleanup without delaying the user's experience.
Click to reveal answer
beginner
How do you make a middleware terminable in Laravel?
To make middleware terminable, add a public method named terminate to your middleware class. Laravel will call this method after the response is sent.
Click to reveal answer
beginner
When is the terminate method of terminable middleware called?
The terminate method is called after the HTTP response is sent to the client, allowing background tasks to run without slowing down the response.
Click to reveal answer
intermediate
Why use terminable middleware instead of regular middleware?
Terminable middleware lets you run code after the response is sent, so users get faster responses. Regular middleware runs before sending the response and can slow down the user experience.
Click to reveal answer
intermediate
Show a simple example of a terminable middleware class in Laravel.
use Closure;
class LogAfterResponse {
public function handle($request, Closure $next) {
return $next($request);
}
public function terminate($request, $response) {
// Code here runs after response is sent
// Example: Log request info
}
}
Click to reveal answer
What method must a Laravel middleware implement to be terminable?
AhandleAfter
Bfinish
Cterminate
Dend
✗ Incorrect
The method named 'terminate' is called after the response is sent to run terminable middleware code.
When does terminable middleware run its terminate method?
ABefore the response is sent
BAfter the response is sent
CDuring routing
DOnly on errors
✗ Incorrect
Terminable middleware runs its terminate method after the HTTP response has been sent to the client.
Which of these is a good use case for terminable middleware?
AValidating user input
BModifying the response content
CRedirecting unauthenticated users
DLogging request details after response
✗ Incorrect
Logging after the response is sent is a common use case for terminable middleware.
What happens if you don't define a terminate method in your middleware?
AMiddleware runs only before response
BMiddleware will not run at all
CMiddleware runs twice
DLaravel throws an error
✗ Incorrect
Without a terminate method, middleware only runs before the response is sent.
Can terminable middleware delay the user's response time?
ANo, it runs after response is sent
BOnly on POST requests
COnly if it has heavy code
DYes, always
✗ Incorrect
Terminable middleware runs after the response is sent, so it does not delay the user's response time.
Explain what terminable middleware is and why it is useful in Laravel.
Think about tasks you want to do after the user sees the page.
You got /4 concepts.
Describe how to create a terminable middleware class in Laravel with an example.
Show the two methods handle and terminate in your example.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of terminable middleware in Laravel?
easy
A. To validate user input before processing
B. To modify the HTTP request before it reaches the controller
C. To handle database migrations automatically
D. To run code after the HTTP response has been sent to the user
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 D
Quick Check:
Terminable middleware = post-response code [OK]
Hint: Terminable middleware runs after response is sent [OK]
Common Mistakes:
Confusing terminable middleware with regular middleware
Thinking it runs before the controller
Assuming it handles request validation
2. Which method must be implemented in a Laravel terminable middleware class?
easy
A. handle
B. process
C. terminate
D. finish
Solution
Step 1: Recall terminable middleware interface
Terminable middleware requires a terminate method to run code after response.
Step 2: Differentiate from handle method
handle runs before response, terminate runs after.
Final Answer:
terminate -> Option C
Quick Check:
Post-response method = terminate [OK]
Hint: Terminate method runs after response in terminable middleware [OK]
Common Mistakes:
Using handle instead of terminate for post-response code
Assuming process or finish are valid methods
Not implementing terminate method at all
3. Given this terminable middleware code, what will be logged after the response is sent?
class LogAfterResponse {
public function handle($request, Closure $next) {
return $next($request);
}
public function terminate($request, $response) {
Log::info('Response sent with status: ' . $response->status());
}
}
medium
A. Logs the request URL before sending the response
B. Logs the response status code after sending the response
C. Throws an error because terminate method is missing parameters
D. Does nothing because terminate is not called automatically
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 using Log::info.
Final Answer:
Logs the response status code after sending the response -> Option B
Quick Check:
terminate logs status post-response [OK]
Hint: Terminate runs after response, so logs status then [OK]
Common Mistakes:
Thinking terminate runs before response
Assuming terminate is not called automatically
Confusing request and response parameters
4. Identify the error in this terminable middleware code:
class CleanupMiddleware {
public function handle($request, Closure $next) {
return $next($request);
}
public function terminate() {
// cleanup code here
}
}
medium
A. terminate method must accept $request and $response parameters
B. handle method must return a string, not a response
C. terminate method should be named handleTerminate
D. handle method must not call $next
Solution
Step 1: Check terminate method signature
Terminable middleware's terminate method must accept both $request and $response parameters.
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 A
Quick Check:
terminate signature requires request and response [OK]
Hint: Terminate method needs both request and response parameters [OK]
Common Mistakes:
Omitting parameters in terminate method
Renaming terminate method incorrectly
Misunderstanding handle method return type
5. You want to log the total time taken to process a request including after the response is sent. How would you implement this using terminable middleware?
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?
}
}
hard
A. $duration = microtime(true) - $this->startTime;
\Log::info('Request took ' . $duration . ' seconds');
B. $duration = microtime(true) + $this->startTime;
\Log::info('Request took ' . $duration . ' seconds');
C. return $this->startTime; // terminate must return a value
D. Call $this->handle($request, $next) again inside terminate
Solution
Step 1: Calculate duration in terminate method
Use current time minus stored start time to get total duration.
Step 2: Log the duration
Use Log::info to record the time taken after response is sent.
Final Answer:
$duration = microtime(true) - $this->startTime;
\Log::info('Request took ' . $duration . ' seconds'); -> Option A
Quick Check:
Duration = now - startTime, then log [OK]
Hint: Subtract start time from current time in terminate to get duration [OK]
Common Mistakes:
Adding times instead of subtracting
Returning values from terminate method
Calling handle method inside terminate
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.