🔥 CodeIgniter Intermediate

What is a BaseController in CodeIgniter 4?

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.