In CodeIgniter 4, Filters act like middleware. They allow you to run code before or after a request, commonly used for authentication, authorization, CSRF protection, and request modification.
Code executed before or after a controller runs
Used to protect routes
Similar to middleware in Laravel
Use Spark CLI:
This creates:
File:
Apply to all requests.
Filter:
| Filter | Purpose |
|---|---|
| csrf | CSRF protection |
| toolbar | Debug toolbar |
| honeypot | Spam protection |
| forcehttps | Force HTTPS |
✅ Use filters for authentication & authorization
✅ Keep filters lightweight
✅ Apply filters at route-level
❌ Avoid heavy logic in filters
Filters = Middleware in CI4
Used before/after requests
Protect routes & APIs
Centralized request control
Take quizzes related to this topic and see where you stand!
Start Quiz Now