Environment driven database settings in CodeIgniter?

Have you ever developed your application locally, then taken it live on your production server and had to manually change your app configuration to match the production server? If you have, then continue reading and learn how configuring environments in CodeIgniter can save you from this headache.


Software projects are typically deployed in various environments. You likely develop your application locally in a development environment. You then have your live server where your site resides, known as the production environment. Many applications will have a staging environment, essentially a duplicate of your production environment, where apps are deployed and tested before being pushed to production. Larger applications may have other environments in between.

Each of these environments may require different configuration, and luckily we can easily handle that in CodeIgniter.

Setp1 : go to index.php file add the fallowing switch statement

Then again add on other switch statement to pick current environment type.

You can also set database connection values for specific environments by placing database.php it the respective environment config folder. For Example : create new folders with your environment names in config folder then copy past the default database.php file in that, then change the settings. Follow the bellow folder structure.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status