Full Stack Software Developer

2020: Making CodeIgniter Modular

This is an implementation that I did recently using CodeIgniter framework for a project. It uses the default implementation of CodeIgniter and adds modularity. Out of the box CodeIgniter framework has `application` and `system` folder at the root. We will use multiple copies of the `application` folder as separate modules which will share the same `system` folder.

First, we have to create a new folder named `applications` and place the existing `application` folder inside the `applications` folder. We will then rename the `application` folder to `module1` folder for ease of understanding. Then make a copy of the `module1` folder and rename it to `module2` folder.

Next, we open up the `index.php` file at the base of the CodeIgniter framework folder and make these changes to the file by line number.

In index.php line 2: Add these lines

$modules = [
	'module1',
	'module2
];

foreach($modules as $current_module)
{
	if(strrpos($_SERVER['REQUEST_URI'], $current_module) !== false)
	{
		$module = $current_module;
		break;
	}
}

if(sizeof(explode('/',$_SERVER['REQUEST_URI'])) > 3)
{
	$_SERVER['REQUEST_URI'] = str_replace('/'.$module.'/', '/', $_SERVER['REQUEST_URI']);
}

if(empty($module) || $module == '/') $module = 'module1';

In index.php at around line 115: update this line

// Update line $application_folder = 'application';
$application_folder = 'applications/'.$module;

In index.php at around line 233: Add this line

// Add after line define('SYSDIR', basename(BASEPATH));
define('MODPATH', FCPATH.'modules');

The configuration is done, the only thing left is to create a .htaccess file at the base of the framework root folder with the following content.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

How it works ?

Basically it updates the application folder based on the URL passed to browser. So, browsing `http://localhost/module1` will render content from `module1` application and `http://localhost/module2` will render content from `module2` application. If user browses `http://localhost/` then it will fall back to the default module mentioned at the top of the index.php file.

How to share classes / models / helpers ?

By default CodeIgniter loader class comes with a method called `add_package_path()` to load external packages. You can use the same method to point to different applications and it will load perfectly. Example of loading a library from `module2` inside the `module1` is given below:

// This MODPATH is defined in our modification of the index.php file
$this->load->add_package_path(MODPATH.'/app2');		
$this->load->library('userlib');
$demo = new userlib();
print $demo->test();

Note: You should use `remove_package_path()` method after you have loaded the needed external libraries to revert back to default path.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.