0
0
Laravelframework~10 mins

Controller middleware in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Controller middleware
Request comes in
Controller Middleware
Pass
Controller Action
Send Response
The request first hits the controller middleware which can either allow it to continue to the controller action or block it and return a response immediately.
Execution Sample
Laravel
public function __construct()
{
    $this->middleware('auth');
}

public function index()
{
    return view('dashboard');
}
This code applies the 'auth' middleware to the controller, so only authenticated users can access the index method.
Execution Table
StepActionMiddleware CheckResultNext Step
1Request arrives at controllerCheck 'auth' middlewareUser authenticated?Yes -> Proceed, No -> Block
2User authenticated?YesPass middlewareCall index() method
3Call index() methodNo middleware checkReturn dashboard viewSend response
4Send responseNo middleware checkResponse sent to userEnd
5If user not authenticatedNoBlock requestRedirect to login
💡 Execution stops when response is sent or request is blocked by middleware.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
RequestIncoming HTTP requestChecked by middlewarePassed to controllerHandled by index()Response sent
User AuthenticatedUnknownCheckedTrue or FalseN/AN/A
Key Moments - 2 Insights
Why does the middleware block the request before the controller method runs?
Because middleware acts as a gatekeeper. If the user is not authenticated (see execution_table step 5), the middleware stops the request and redirects before the controller method executes.
Can middleware modify the request or response?
Yes, middleware can change the request before passing it on or modify the response before sending it back, but in this example it only checks authentication (execution_table steps 1-2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2 if the user is authenticated?
AThe middleware passes the request to the controller
BThe middleware blocks the request
CThe request is redirected to login
DThe response is sent immediately
💡 Hint
Check the 'Result' and 'Next Step' columns in execution_table row 2
At which step does the controller method 'index' get called?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table row 3
If the user is not authenticated, what does the middleware do according to the execution table?
APasses the request to the controller
BReturns the dashboard view
CBlocks the request and redirects to login
DSends the response immediately
💡 Hint
See execution_table row 5 under 'Result' and 'Next Step'
Concept Snapshot
Controller middleware in Laravel acts as a filter for requests.
It runs before controller methods.
Middleware can allow or block requests.
Example: 'auth' middleware blocks unauthenticated users.
Middleware can redirect or modify requests/responses.
Applied in controller constructor with $this->middleware('name').
Full Transcript
In Laravel, controller middleware runs before the controller's methods. When a request arrives, the middleware checks conditions like user authentication. If the user passes the check, the request proceeds to the controller method, which then returns a response. If the user fails, the middleware blocks the request and can redirect the user, for example, to a login page. This process ensures only authorized users access certain controller actions. Middleware is applied in the controller's constructor using $this->middleware('auth'). This flow helps keep your application secure and organized.