Asif18

Author:
{ Comments }

Hi Guys, after a looong time i started to post articles to the blog. In this article am explaining about how to create multiple applications in single CodeIgniter instance. Yeah, Laravel is more popular than CI. This is for those CI Developers who may required. If you are developing single application using codeigniter, single codeigniter instance is enough. I got a recent requirement from my client that i need to develop multiple applications (sub-apps such as admin/site/billing etc.,), the database is same but the application will be hosted in different sub-domains BUT with single codeigniter instance. I felt actually weird to install fresh codeigniter package to each applications. This is leading to duplication and hard to maintain. So i decided to go with single codeigniter instance (Single CodeIgniter System Folder) but with multiple applications. A major problem problem i faced while running multiple applications with one codeigniter instance is URL Writing in .htaccess.

multiple applications with codeigniter php

Assume, creating site or frontend application and admin application in a single codeigniter instance. For the site or frontend folder I left the CodeIgniter index.php as it is. But for the admin application I have copy pasted the same codeigniter index.php and renamed it to admin.php. Now whenever I open the application (since index.php is default index) the frontend part will run with no issues. As same in the URL when i type admin.php the admin application will run with no issues. BUT the actual problem really is here with removing the index.php and admin.php from the URL.

I tried with the normal .htaccess code that used to remove index.php from the URL. It works like charm in frontend but not in the admin folder. I will tell you the way how i resolved this issue.

Steps to follow

  1. Download latest CodeIgniter
  2. Extract into your server folder.
  3. Create new folders such as admin, site and billing (For this example).
  4. Cut and paste the CI Application folder into the admin, site and billing folders which you created now.
  5. Now, copy and paste CI index.php file only to the admin and billing folders.
  6. Carefully open the CI index.php in the main directory which is showing in the below first 'folder structure' image,
  7. Find and edit the variable like this: $application_folder = 'application'; to $application_folder = 'site/application';
  8. Now, goto the admin and billing folders and open the respective CI index.php files that you did in the 5th point,
  9. Find and edit the variable like this: $system_path = 'system'; to $system_path = '../system';

Now the folder setup is completed gracefully. Will use simple RewriteRule to remove index.php from the URL. 

.htaccess file for removing index.php from URL

RewriteEngine on

# send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Paste this .htaccess file in the parent folder and only in 'admin' and 'billing' folders.

Finally my folder structure will be like below,

Folder Structure - CodeIgniter

codeigniter-folder-structure-admin

Billing Folder Structure - CodeIgniter

Site Folder Structure - CodeIgniter

I got the clean URL's, now the application is working good and on production. This solution works on sub-domains, sub-directories or wherever. 

I have installed CI 3.1.6 in the demo. You can test it with below URL's.

http://demo.asif18.com/PHP/multiple-applications-codeigniter/
http://demo.asif18.com/PHP/multiple-applications-codeigniter/sample/param
http://demo.asif18.com/PHP/multiple-applications-codeigniter/admin
http://demo.asif18.com/PHP/multiple-applications-codeigniter/admin/sample/param
http://demo.asif18.com/PHP/multiple-applications-codeigniter/billing
http://demo.asif18.com/PHP/multiple-applications-codeigniter/billing/sample/param

END of the Line for complete working solution.

I tried few ways too to achieve the above by the help of Google and Stackoverflow. But didn't work as I was expected. Few solutions were working good in localhost but not on the server after deployment. So combining the Google and Stackoverflow responses i applied the above logic.

Your thoughts/suggestions/comments may help others. Peace :)

Comments (2)