Lesson 20 โข Advanced
Middleware Architecture ๐
Process HTTP requests through a stack of middleware for authentication, logging, CORS, rate limiting, and more.
What You'll Learn in This Lesson
- โข What middleware is and how request pipelines work
- โข Build a chainable middleware pipeline from scratch
- โข Create auth, CORS, rate limiting, and logging middleware
- โข Apply middleware conditionally to specific routes
- โข Middleware ordering and the difference from filters/events
Building a Middleware Pipeline
A middleware pipeline processes requests through a chain of functions. Each middleware receives the request and a $next callback. It can modify the request, call $next to continue, or return early to short-circuit the pipeline (e.g., rejecting unauthenticated requests).
Try It: Middleware Pipeline
Build a pipeline with logging, CORS, rate limiting, and auth middleware
// Middleware Pipeline: Processing Requests
console.log("=== What is Middleware? ===");
console.log();
console.log("Middleware = functions that process a request BEFORE it reaches");
console.log("your controller. Each middleware can:");
console.log(" 1. Modify the request (add data, parse headers)");
console.log(" 2. Short-circuit (reject unauthorized requests)");
console.log(" 3. Modify the response (add headers, compress)");
console.log(" 4. Pass to the next middleware in the stack");
cons
...Practical Middleware Patterns
In production, you'll apply different middleware to different routes. Public pages need CORS and logging but not authentication. API endpoints need auth and rate limiting. Admin routes need both auth and admin-role checks. Learning to compose middleware stacks is a core skill for framework-level PHP development.
Try It: Practical Middleware
Apply middleware conditionally to routes and understand ordering
// Practical Middleware Examples
console.log("=== Common Middleware Stack ===");
console.log();
console.log("Order matters! Typical stack:");
console.log(" 1. Error Handler โ Catch exceptions from all layers");
console.log(" 2. CORS โ Handle cross-origin requests");
console.log(" 3. Rate Limiter โ Throttle excessive requests");
console.log(" 4. Body Parser โ Parse JSON/form bodies");
console.log(" 5. Authentication โ Verify JWT/session tokens");
console.log("
...โ ๏ธ Common Mistakes
$next($request), the request stops there. Only skip it intentionally (like rejecting unauthenticated requests).'web', 'api') to apply common stacks. Custom middleware should implement MiddlewareInterface for consistency.๐ Quick Reference โ Middleware
| Middleware | Purpose |
|---|---|
| Auth | Verify JWT/session, attach user to request |
| CORS | Add cross-origin headers for browser requests |
| Rate Limiter | Throttle requests per IP/user |
| Logger | Log request method, path, timing |
| Body Parser | Parse JSON/form request bodies |
| Error Handler | Catch exceptions, return formatted errors |
๐ Lesson Complete!
You understand middleware pipelines! Next, dive into advanced PDO with transactions, prepared statements, and stored procedures.
Sign up for free to track which lessons you've completed and get learning reminders.