🔥 CodeIgniter Intermediate

What is a BaseController in CodeIgniter 4?

Why Interviewers Ask This

This question targets practical, hands-on experience with CodeIgniter. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.

Answer

The BaseController in CodeIgniter 4 (app/Controllers/BaseController.php) is an abstract controller that all other controllers extend. It extends CI4's Controller class and is the perfect place to put shared initialization code. The initController() method is called before any controller method — use it to load helpers, initialize properties, or set up shared services: helper(["url", "form"]), $this->session = service("session"). All controllers that extend BaseController inherit these shared resources. Add common middleware checks here (e.g., check if the user has accepted terms). This avoids duplicating initialization across every controller. Keep the BaseController lean — only put things that every controller needs. Create specialized base controllers for specific sections: AdminBaseController, ApiBaseController.

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.