0
0
Laravelframework~10 mins

Global middleware in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Global middleware
Request enters Laravel
Global Middleware runs
Controller or Route executes
Response generated
Global Middleware runs on Response
Response sent to client
Global middleware runs automatically on every HTTP request before and after the main route or controller handles it.
Execution Sample
Laravel
<?php
// app/Http/Kernel.php
protected $middleware = [
    \App\Http\Middleware\CheckForMaintenanceMode::class,
];
This code registers a global middleware that runs on every request to check if the app is in maintenance mode.
Execution Table
StepActionMiddleware StateRequest/ResponseNotes
1Request enters LaravelNo middleware run yetIncoming HTTP RequestStart of request lifecycle
2Global middleware CheckForMaintenanceMode runsMiddleware checks app statusRequest passed through middlewareMiddleware can block or allow request
3Controller or Route executesMiddleware passedController processes requestMain app logic runs
4Response generated by controllerMiddleware passedResponse object createdResponse ready to send
5Global middleware runs on ResponseMiddleware can modify responseResponse passed back through middlewareMiddleware can add headers or modify content
6Response sent to clientMiddleware finishedFinal HTTP Response sentRequest lifecycle ends
💡 Response sent to client, request lifecycle complete
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
RequestRaw HTTP requestChecked for maintenance modePassed to controllerPassed back through middlewareSent to client
ResponseNoneNoneCreated by controllerModified by middlewareFinal response sent
Key Moments - 3 Insights
Why does global middleware run on every request?
Because it is registered in the $middleware array in Kernel.php, Laravel automatically runs it for all requests as shown in execution_table steps 2 and 5.
Can global middleware stop a request before it reaches the controller?
Yes, as seen in step 2, middleware can block the request (for example, if the app is in maintenance mode) and prevent controller execution.
Does global middleware run on the response as well as the request?
Yes, step 5 shows middleware running on the response to modify it before sending to the client.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the controller process the request?
AStep 2
BStep 5
CStep 3
DStep 6
💡 Hint
Check the 'Action' column in execution_table row 3.
At which step does the global middleware check if the app is in maintenance mode?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Middleware State' column in execution_table row 2.
If the middleware blocked the request at step 2, what would happen next?
AResponse is generated immediately by middleware
BController runs anyway
CRequest is sent to client without response
DMiddleware runs again on response
💡 Hint
Refer to key_moments about middleware blocking requests before controller.
Concept Snapshot
Global middleware runs on every HTTP request in Laravel.
It is registered in app/Http/Kernel.php in the $middleware array.
Middleware runs before the controller and after response generation.
It can modify or block requests and responses.
Useful for tasks like maintenance mode checks or adding headers.
Full Transcript
Global middleware in Laravel is code that runs automatically on every HTTP request. When a request comes in, Laravel first runs all global middleware registered in the Kernel.php file. These middleware can check conditions like if the app is in maintenance mode and block the request if needed. If allowed, the request goes to the controller or route to generate a response. After the response is created, the global middleware runs again on the response to modify it if needed, such as adding headers. Finally, the response is sent back to the client. This process ensures middleware logic applies to all requests consistently.