0
0
Laravelframework~10 mins

Creating middleware in Laravel - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating middleware
Request enters app
Middleware invoked
Middleware checks condition
Allow request
Controller runs
Response returns to client
The request passes through middleware which checks conditions and either allows it to continue or blocks it.
Execution Sample
Laravel
php artisan make:middleware CheckAge

public function handle($request, Closure $next) {
  if ($request->age < 18) {
    return redirect('home');
  }
  return $next($request);
}
This middleware checks if the user's age is less than 18 and redirects if true, otherwise it lets the request continue.
Execution Table
StepActionCondition CheckedResultNext Step
1Request enters middlewareN/AMiddleware startsCheck age condition
2Check if age < 18age = 16TrueRedirect to home
3Redirect response sentN/ARequest blockedEnd
4Request endsN/ANo controller reachedEnd
💡 Request blocked because age < 18 condition was true, so middleware redirected.
Variable Tracker
VariableStartAfter Step 2Final
ageunknown1616
request_statusincomingcheckedredirected
Key Moments - 2 Insights
Why does the request stop and not reach the controller?
Because at step 2 in the execution_table, the condition age < 18 is true, so middleware returns a redirect response instead of calling $next($request).
What does $next($request) do in middleware?
It passes the request to the next middleware or the controller. Without calling $next, the request stops in middleware.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 2 when age is 16?
AThe request is allowed to continue to the controller
BThe request is redirected to home
CThe middleware throws an error
DThe request is logged but continues
💡 Hint
Check the 'Result' column in step 2 of execution_table.
At which step does the middleware stop the request from continuing?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for when the redirect response is sent in execution_table.
If age was 20, how would the execution_table change?
AStep 2 condition would be false and request continues
BStep 3 redirect would still happen
CMiddleware would throw an error
DRequest would be blocked at step 1
💡 Hint
Think about the condition age < 18 in step 2.
Concept Snapshot
Creating middleware in Laravel:
- Use 'php artisan make:middleware Name' to create.
- Middleware handle() checks request conditions.
- Return redirect or response to block.
- Call $next($request) to continue.
- Middleware runs before controller.
- Useful for auth, logging, filters.
Full Transcript
Middleware in Laravel acts like a gatekeeper for requests. When a request comes in, it first passes through middleware. The middleware checks conditions, like if the user's age is less than 18. If the condition is true, the middleware can stop the request and redirect it elsewhere, like to the home page. If the condition is false, the middleware calls $next($request) to let the request continue to the controller. This process helps control access and modify requests before they reach the main app logic.