Inheriting a Base Controller In CodeIgniter
Introduction
So sometimes you want to have a base controller class where you can store common methods through out all your controllers. Here’s how you do it.
Steps
Writing the Inheritable Controller
All you need to do is write the file application/core/MY_Controller.php
Then you should should write separate controllers depending on your methods, for example I will write application/core/Main_Controller.php
Auto-loading the Controllers
We need to ammend config.php
to include all the controllers in the application/core
directory. Note that means all these controllers will be loaded you will fallout of CodeIgniter conventions if you do so. Make sure you absolutely need these core functions or else you’ll be loading in garabe you don’t need on every request.
Finishing up
Lastly, you need to actually use your new extended controller!