Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a terminable middleware class in Laravel.
Laravel
class LogAfterRequest { public function handle($request, Closure $next) { return $next($request); } public function [1]($request, $response) { // Code to run after response is sent } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'handleAfter' or 'finish' instead of 'terminate'.
Not defining the terminate method at all.
✗ Incorrect
The method to define in terminable middleware is 'terminate'. Laravel calls this method after the response is sent.
2fill in blank
mediumComplete the code to register the terminable middleware in the HTTP kernel.
Laravel
protected $middleware = [
// Other middleware
[1]::class,
]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names that don't exist.
Registering middleware in the wrong property like $routeMiddleware.
✗ Incorrect
You register the terminable middleware class name in the $middleware array in the HTTP kernel.
3fill in blank
hardFix the error in the terminable middleware method signature.
Laravel
public function [1](Request $request, Response $response) {
// post-response logic
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'handle' instead of 'terminate'.
Using incorrect method names like 'finish'.
✗ Incorrect
The correct method name for terminable middleware is 'terminate' and it accepts the request and response.
4fill in blank
hardFill both blanks to correctly call the next middleware and define the terminable method.
Laravel
public function handle($request, Closure $next) {
return [1]($request);
}
public function [2]($request, $response) {
// post-response logic
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'handle' instead of $next in the handle method.
Naming the terminable method incorrectly.
✗ Incorrect
The handle method calls $next($request) to pass the request along. The terminable method is named 'terminate'.
5fill in blank
hardFill all three blanks to create a terminable middleware that logs after response is sent.
Laravel
class LogAfterRequest { public function [1]($request, Closure $next) { return $next([2]); } public function [3]($request, $response) { // Log something here } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names like 'finish' or 'handleAfter'.
Passing wrong variables to $next.
✗ Incorrect
The handle method is named 'handle' and calls $next($request). The terminable method is 'terminate'.