How I use Codeigniter to Manage Multiple Versions of the app and switch in seconds?

Dheeraj jha
3 min readDec 25, 2017

--

I must say CodeIgniter is amazing PHP/MVC framework that helps me to do almost anything to the deepest flexible way I had to do. Initially, I kept using it for one application only but now, I am with little help of various options provided by CodeIgniter and a bit of use of these, I can manage multiple CodeIgniter application/versions or say an application that I can access with different sub-domain or URLs like the charm.

All those versions may have a separate application, database, base URL, session configuration and much more.

Setting up config/database.php

You might not have noticed or used but CodeIgniter lets you manage multiple database configuration settings that you can switch using one single variable to tell which one is active DB setting.

By default, you can see single configuration array like this

Notice this configuration is in the array $db[‘local’], you can create copies of this array with different name and different database configuration and name $db[‘production’] and $db[‘development’] etc.

on this page, you can notice a variable $active_group = ‘default’;

You can put a constant word say

$active_group = DB_GROUP; which you can be defined based on your configuration selection, this will allow you to dynamically choose DB array from a list of configuration you have in the database.php file.

Modify config/config.php

You can modify 2 3 variables here

Just see first configuration setting, and make it look like

$config[‘base_url’] = BASE_URL;

Here we have modified its value to a constant which we will define.

Scroll below and find session configuration which you can define the path for the session in config variable $config[‘sess_save_path’].

Now move to /index.php file and see how magically you can set up stuff right here.

Modify index.php file

Scroll down to set application folder name as

$application_folder = APP_DIR;

Now on top of the index.php file

Do the following

This way you have done some awesome work.

How it works

Your index.php first of all is going to see from which URL request is coming from, based on it, it switches between 3 4 or even more defined settings to set the following information dynamically.

  • Database configuration settings
  • Set the dynamic base URL
  • Set dynamic server environment configuration
  • Set application folder that will be used for your application in the given environment/URL.

You can have as many application using same CodeIgniter system folder settings but different application folder, running on the same server yet utilizing multiple URLs even without doing any configuration of the virtual host. ;-)

How to Manage multiple application

Suppose you have two URL.

staging.example.com and example.com

staging points to a dev_app folder of application folder whole example.com point to a live_app folder.

You can do switch by either renaming dev_app to live_app or simply defined APP_DIR as dev_app for live server and you can do renaming and make live_app and dev_app as required.

Share this article and let me know what else we can add up to make ci more amazing to use.

Thanks for your time.

--

--

Dheeraj jha

DevOps Engineer | Team lead | AWS | Docker | CI/CD | Gitlab-CI