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 increase 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.

What next?

4 responses to “CodeIgniter Advent: Day 1”

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

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

[...] out CodeIgniter Advent on Michael Wales’ [...]

[...] copy a blank application folder over when I need a new project (finally picked that up from reading Michael Wales’ blog). While I don’t have many projects going on, I’m working on getting more frequent [...]

Leave a comment

If you like, you can format your comment using these HTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

You can even comment with a code snippet from Pastie by using: <pastie:##### /> - replacing the #'s with the Pastie ID of the snippet.