0
0
Laravelframework~10 mins

Route middleware in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route middleware
Request comes in
Route matched
Middleware stack starts
Middleware 1: Check condition
Yes No
Pass
Middleware 2: Next check
Controller or final handler
Response sent back
When a request matches a route, Laravel runs middleware in order. Each middleware can allow the request to continue or stop it with a response.
Execution Sample
Laravel
Route::middleware(['auth', 'verified'])->get('/dashboard', function () {
    return 'Welcome!';
});
This route uses 'auth' and 'verified' middleware before showing the dashboard.
Execution Table
StepMiddlewareCondition CheckedResultAction TakenNext Step
1authIs user logged in?YesPass request to next middlewareGo to Step 2
2verifiedIs user email verified?YesPass request to controllerGo to Step 3
3controllerN/AN/AReturn 'Welcome!'Send response
ExitN/AN/AN/AResponse sent back to clientEnd
💡 Middleware passed all checks; controller returned response; response sent to client.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
request_statusIncoming requestAuthenticatedVerifiedHandled by controller
responseNoneNoneNone'Welcome!' string
Key Moments - 3 Insights
What happens if the 'auth' middleware fails?
If 'auth' fails (user not logged in), it stops the request and returns a redirect or error immediately, so later middleware and controller do not run. See Step 1 in execution_table.
Can middleware modify the request before passing it on?
Yes, middleware can change the request or add data before passing it to the next middleware or controller. This happens between steps in the middleware stack.
Why is middleware order important?
Middleware run in the order listed. If an early middleware blocks the request, later ones never run. So order controls which checks happen first.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the 'auth' middleware at Step 1?
AUser is authenticated, request passes on
BUser is not authenticated, request blocked
CUser email is verified
DController returns response
💡 Hint
Check the 'Result' column for Step 1 in execution_table
At which step does the controller handle the request?
AStep 1
BStep 2
CStep 3
DExit
💡 Hint
Look for 'controller' in the Middleware column in execution_table
If the 'verified' middleware fails, what happens next?
ARequest continues to controller
BRequest is blocked and response sent immediately
CNext middleware runs
DRequest is retried
💡 Hint
See the branching logic in concept_flow and Step 2 in execution_table
Concept Snapshot
Route middleware runs before the controller.
Each middleware checks conditions.
If a middleware fails, it stops the request.
Middleware run in the order listed.
If all pass, controller handles the request.
Response then returns to client.
Full Transcript
When a web request comes to Laravel, it first finds the matching route. Before running the route's controller, Laravel runs middleware in the order they are listed. Each middleware checks something, like if the user is logged in or email verified. If a middleware check fails, it stops the request and sends a response, like a redirect or error. If it passes, the request moves to the next middleware. After all middleware pass, the controller runs and returns a response. This response then goes back to the user. Middleware order matters because the first failure stops everything else.