Skip to content

Multiple Applications

Derek Jones edited this page Jul 5, 2012 · 26 revisions

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

CodeIgniter is distributed with one application in mind, but is ready to accommodate multiple applications on a given site. However, because the /application/ folder is inside of the /system/ 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:

CodeIgniter_x.x.x/
    license.txt -- the usage licence agreement
    index.php   -- your front controller, 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

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

  1. a frontend application (public site)
  2. a backend application (admin site)

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

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

Duplicate the contents of your /application/ folder, and put the copies into separate subfolders, so you may end up with this layout:

/system/application/
            /frontend/
            /backend/

You would need one index.php file per application, and you would simply update the $application_folder 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. According to the term paper written, 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]Method #2 - Separate Application Folders[/size]

This method involves changing the default layout of the files by moving the /application/ folder out of the /system/ folder.

We still need to duplicate the application folder.

We can lay it out like this, which is my personal preference:

/application/
    /frontend/
    /backend/
/system/

or like this:

/frontend/
/backend/
/system/

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

[size=4]Accessing The Application[/size]

/frontend/
/backend/
/system/
frontend.php
backend.php

Both frontend.php & backend.php are duplicate of the index.php, where you'll need to update the $application_folder in each file pointed to the corresponding folder

and your url to access each application will be

http://domain.com/frontend.php
http://domain.com/backend.php

[size=4]Review & Some Final Thoughts[/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 /system/ and /application/ folders out of the webroot, while leaving the index.php file in the webroot (obviously). You just need to adjust the settings in the index file to map correctly and you're all set.

/application/
    /frontend/
    /backend/
/system/
/webroot/
    index.php

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 /system/, I instead use CodeIgniter_x.x.x/system/, as shown here:

/CodeIgniter_x.x.x/
    /system/
/application/
    /frontend/
    /backend/

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.

One last step: You have to edit ../your_application/config/config.php and change $config['index_page'] = "your_application_index.php" If you do not do it, links will be wrong on your scaffolded pages.

Translater

Clone this wiki locally