0
0
Laravelframework~10 mins

Terminable middleware in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AhandleAfter
BafterResponse
Cterminate
Dfinish
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'handleAfter' or 'finish' instead of 'terminate'.
Not defining the terminate method at all.
2fill in blank
medium

Complete 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'
ALogAfterRequest
BTerminateMiddleware
CAfterResponseMiddleware
DTerminateRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names that don't exist.
Registering middleware in the wrong property like $routeMiddleware.
3fill in blank
hard

Fix 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'
Afinish
Bhandle
CterminateRequest
Dterminate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'handle' instead of 'terminate'.
Using incorrect method names like 'finish'.
4fill in blank
hard

Fill 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'
A$next
Bterminate
Chandle
Dfinish
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'handle' instead of $next in the handle method.
Naming the terminable method incorrectly.
5fill in blank
hard

Fill 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'
Ahandle
B$request
Cterminate
D$response
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names like 'finish' or 'handleAfter'.
Passing wrong variables to $next.