CodeIgniter Advent: Day 1

As CodeIgniter developers we are blessed with a very quick “up and running” time - since CodeIgniter doesn’t really require a bootstrap file or a lot of configuration. A few changes within your application’s ./config directory and you are on your way. But, there are always ways to speed up the process and I’m going to show you how I personally manage my development environment and get projects up and running.

Whether you are developing locally or you have live applications on your server, one of the easiest ways to speed up your development time is by separating the CodeIgniter core files from your application files. By default, CodeIgniter places your application within the ./system/application directory. By seperating the core from our application we’ll not only decrease development time but our application will immediately become more secure (since we can now place CodeIgniter’s core files above our webroot).

For the sake of clarity, this tutorial is assuming your are developing on a live server in which each of you domains is reflected as a directory off of the server’s root.

The first thing we’ll do is move our ./system/application directory outside of the ./system folder (just for the sake of clarity, we’re going to place this at the same level as ./system. Your filesystem should now look like this (based on a fresh CodeIgniter installation on a live server):

./application
./system
./user_guide
./index.php
./license.txt

Since most developers have numerous projects running at one time (I know I do), we’re going to keep the ./application directory around and make a few changes to it that will affect all of our future applications. By doing this, we’ll just be able to make a copy of the ./application directory for each of our new projects. Let’s rename this directory to ./_base to reflect it’s new role.

Additionally, when a user visits one of our domains they will need to hit our bootstrap file (./index.php). This is the file that performs the initial CodeIgniter load and sends the PHP processor off to our application’s PHP files. We’re going to need a bootstrap file for each of our applications so let’s move it into our new./_base/htdocs directory. Finally, in keeping with the CodeIgniter license requirements, each of our applications needs to be distributed with a copy of ./license.txt so move it into the ./_base directory as well.
Since CodeIgniter is regularly updated we may want to run multiple versions of CodeIgniter on our server. For this reason, we’ll rename our ./system directory to ./ci-1.7.0. This will also allow you to update your applications to the latest version of the framework with a one line change in that applications ./htdocs/index.php file. Our filesystem now looks like:

./_base
./_base/license.txt
./_base/htdocs/index.php
./ci-1.7.0
./user_guide

As I previously mentioned, our application’s bootstrap file (./_base/htdocs/index.php) hands the PHP processor off to our application specific files. We’ll need to make some minor changes to ./_base/htdocs/index.php to point CodeIgniter in the right direction and tell it where to find our framework files as well as our application’s files. Here are the changes that need to be made to the two variables within this file (and approximate line numbers):

Now, all we need to do to start a new application is to make a copy of our ./_base directory and give it our domain’s name! If we were to start a new CodeIgniter application on the domain michaelwales.com our filesystem would look like this:

./_base
./_base/htdocs/index.php
./ci-1.7.0
./michaelwales/htdocs/index.php
./user_guide

Of course, much of this depends on how your server is configured (in this case, how to serve domains from the filesystem) but take the lessons taught in this post and apply them to your own unique environment and you should have a secure, and quick, CodeIgniter installation.



Comments

  1. Colin December 1st

    Comment Arrow

    This is a testament to when configuration trumps convention. Amazing how much flexibility 2 configurations, $system_folder and $application_folder, afford the entire framework.


  2. Jason Hudnutt December 2nd

    Comment Arrow

    Great start to the advent! Looking forward to the rest of the posts!


  3. Rick January 2nd

    Comment Arrow

    I made the changes according to your instructions but when I browse to the application directory via the browser, I get “Directory access is forbidden.” due to the index.html file in the root of the base. How is the “index.php” file read within the htdocs folder? The URL to the “_base” application on my development box is “http://localhost/frameworks/codeginiter/_base/”, will your settings work for such environment? Please advise.


  4. bahodir January 9th

    Comment Arrow

    rick, I had to change the $system_folder in the index file from $system_folder = ‘/ci-1.7.0′; to $system_folder = ‘../../ci-1.7.0′; to get it working.


  5. Michael Wales January 9th

    Comment Arrow

    Rick,
    It would depend on how your server is configured. Ensure your moved the index.php from the downloaded .zip file into your _base/ directory.

    If it still does not come up, check your server configuration and ensure that index.php is checked for existence prior to index.html, index.htm, or home.html/.htm if a URI is not defined.


  6. Leigh February 4th

    Comment Arrow

    I think you mean something other than what is stated in paragraph 2:

    “By seperating the core from our application we’ll not only increase development time but…”

    But whaddoIknow, I’m a CI noob.


  7. Michael Wales February 5th

    Comment Arrow

    @Leigh
    Good catch, I meant to say “decrease development time” - I’ve changed it within the article.


  8. Prabhjeet Singh March 18th

    Comment Arrow

    This is fantastic article. I love it. Can’t stop to read next articles about it. Thanks for this.


Add Yours

  • Author Avatar

    YOU


Comment Arrow



About Author

Michael Wales

Michael Wales is a Senior Web Developer with a leading government contractor and, more importantly, the Community Chieftain for the CodeIgniter community. Additionally, Michael is working on a book entitled Beginning CodeIgniter that will lead you from application conception to completion. Follow him on Twitter!