Skip to content

Multiple Applications

World Wide Web Server edited this page Jul 4, 2012 · 26 revisions

[size=5][color=blue]Managing Multiple Applications[/color][/size]

CodeIgniter is distributed with one application in mind, but is ready to accommodate multiple applications on a given site. However, because the [color=blue]/application/[/color] folder is inside of the [color=blue]/system/[/color] folder, this leads some to think you need to have multiple installations of the system files. Let's look at how this can be done.

Support for multiple applications is simply a matter of: [b]

  • having multiple application folders
  • having one front controller (index.php) per application, or some other crafty way of mimicking this. [/b]

Let's start by reviewing CodeIgniter's default file layout (currently v1.5.3). When you download the distribution and extract the archive, you end up with this layout: [code] CodeIgniter_x.x.x/ license.txt -- the usage licence agreement index.php -- your front controller, to be installed in the webroot of your site. system/ -- this is where the magic is user_guide/ -- a copy of the User Guide for offline/local reference [/code]

For the following examples, we'll create two applications.

  1. a [b]frontend[/b] application (public site)
  2. a [b]backend[/b] application (admin site)

[size=4][color=red]Method #1 - Application Subfolders[/color][/size]

Inside the [color=blue]/system/[/color] folder lives the [color=blue]/application/[/color], by default. The User Guide describes a technique for having multiple applications by following this process.

Duplicate the contents of your [color=blue]/application/[/color] folder, and put the copies into separate subfolders, so you may end up with this layout: [code] /system/application/ /frontend/ /backend/ [/code]

You would need one index.php file per application, and you would simply update the [color=blue]$application_folder[/color] variable in each to point to the respective folder.

The disadvantage of this application is that your applications are still inside of the system folder, so upgrading must be done carefully. You can't just drop a new version of CodeIgniter into your system folder because you risk overwriting the application folder (which contains your two applications).

A better way would be to move the applications out of the system folder.

[size=4][color=red]Method #2 - Separate Application Folders[/color][/size]

This method involves changing the default layout of the files by moving the [color=blue]/application/[/color] folder [b]out[/b] of the [color=blue]/system/[/color] folder.

We still need to duplicate the application folder.

We can lay it out like this, which is my personal preference: [code] /application/ /frontend/ /backend/ /system/ [/code] or like this: [code] /frontend/ /backend/ /system/ [/code]

So long as you update your index.php files to map to the correct folders, everything will continue to work correctly.

[size=4][color=red]Review & Some Final Thoughts[/color][/size] Now you know how to:

  • create multiple application folders
  • rearrange the folders without breaking the framework
  • improving future upgrading by moving the applications out of the system folder

There are some final points I'd like to include.

CodeIgniter suggests putting the files into the webroot of your website, because that colors the widest range of web servers in use today. However, many servers have the webroot as a subfolder of the account folder. This lets you store files outside the webroot, primarily for added security. Those files won't be accessible through a web browser, but they will be accessible to your PHP code.

Therefore, you can technically move your entire [color=blue]/system/[/color] and [color=blue]/application/[/color] folders out of the webroot, while leaving the [color=blue]index.php[/color] file in the webroot (obviously). You just need to adjust the settings in the index file to map correctly and you're all set. [code] /application/ /frontend/ /backend/ /system/ /webroot/ index.php [/code]

Clone this wiki locally