-
Notifications
You must be signed in to change notification settings - Fork 0
Multiple Applications
[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]
- one system folder
- one application folder per application
- 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 application/ user_guide/ -- a copy of the User Guide for offline/local reference [/code]
For the following examples, we'll create two applications.
- a [b]frontend[/b] application (public site)
- 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 approach 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, which I hope will help to solidify the above explanation even more by seeing it from a different perspective.
CodeIgniter suggests putting the files into the webroot of your website, because that covers 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] Another step that I take is keeping the CodeIgniter_x.x.x folder as part of my layout, so I always know which version I have installed. Instead of just [color=blue]/system/[/color], I instead use [color=blue]CodeIgniter_x.x.x/system/[/color], as shown here: [code] /CodeIgniter_x.x.x/ /system/ /application/ /frontend/ /backend/ [/code]
This lets you download and drop in a new CodeIgniter_x.x.x without disturbing any of your files. Then, just update index.php to point to the new one, follow any other Upgrade steps you need to take, and you're done. Simply remove the old CodeIgniter version.