What are Filters in CodeIgniter 4?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid CodeIgniter basics — a prerequisite for any developer role.
Answer
Filters in CodeIgniter 4 are the equivalent of middleware in Laravel — they run before or after a controller executes. Define a filter by implementing CodeIgniter\Filters\FilterInterface with before() and after() methods. Register filters in app/Config/Filters.php: add to $aliases and assign to $globals (run on every request), $methods (run for specific HTTP methods), or $filters (run for specific URIs). Apply to routes: $routes->get("admin/dashboard", "Admin::index", ["filter" => "auth"]). Built-in filters: csrf (CSRF protection), honeypot (spam protection), invalidchars, forcehttps. The before() method can return a Response to short-circuit the request, and the after() method can modify the response before it is sent.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.