Ultimate Codeigniter Setup
Posted in Codeigniter | Posted on 14-10-2009
Tags: Codeigniter, htaccess, php, simpletest, unit test
6
There are many ways to setup a codeigniter installation, however, I thought it might be useful to share the setup that I currently use.
My setup allow the removal of the index.php from the URL as well as having a front and backend application running off the same codeigniter base. In addition to this the codeigniter base is separated completely from the application code so that it is simpler to upgrade whenever new there are new releases of Codeigniter.
The final thing which works for me is including simpletest in the codeigniter for unit testing. Much kudos to jamie rumbelow for sharing his unit test installation setup on which this is largely based.
Anyway down to business… I should point out here that I use a windows vista with xampp for my development environment.
Codeigniter Install and Directory Structure
- Download and unzip the latest version of CodeIgniter into the folder of your choice – I’ll call mine ‘my_project’
- Your ‘my_project’ folder should now have two folders – system and user_guide plus index.php and license.txt
- You can now delete the user_guide folder or save this off somewhere else as it is not required within your application.
- Within your ’system’ folder there will be a ‘application’ folder. If you can cut this and paste this folder into the root of your ‘my_project’ folder. The reason for doing this is so that the main Codeigniter files are separated from your application to make upgrading easier.
- The next step I usually go for is to rename the ’system’ file to ‘@codeigniter’ so that it always appears at the top of my file directory and is separate from the main application folders. By changing the name of the folder it also increases the security of your system files.
- The next thing we need to do is to create the folder where our back-end application will go so create a new folder and call it ‘admin’.
- Next we need to copy the ‘application’ folder and paste it into the root of your ‘admin’ folder so we should now have two application folders ‘my_project/application’ and ‘my_project/admin/application’. The reason for two systems is because we are going to use one for the front-end and the other for the back-end application. You may only need one depending upon what application you are building.
- I would now rename the ‘application’ folders in the root directory to something more meaningful so I would rename it ‘main’.
- So if you’ve followed the steps so far you should have a directory structure that looks something like this:

Codeigniter Main Configuration
The next thing we need to do is to configure Codeigniter to work correctly initially.
- Open up the file ‘main/config/config.php’ and change $config['base_url'] to the correct url for that location. Mine would be ‘http://localhost/workspace/my_project/’
- Change $config['index_page']=”; – The reason for this is that we will be using .htaccess file to remove the index.php from the urls
- The next file we need to update is the main index.php file in the root of the project folder. Before we do that though we need to copy this file and put a copy of it into the admin folder. The reason for this is that the system will be driven by two separate index.php files. One for the front-end in the root directory and the other in the admin directory.
- Once you’ve done that if you can edit the index.php file in the root directory and change $system_folder = “@codeigniter”; / $application_folder=’main’;
- Now you’ve completed all these steps if you can run your local server and point your browser to project folder url - i.e. http://localhost/workspace/my_project/
- If everything has been successful so far you will see the CodeIgniter welcome page:

CodeIgniter Admin Configuration
We need to complete a similiar exercise to Configure the Admin section so take the following steps.
- Open up the file ‘admin/application/config/config.php’ and change $config['base_url'] to the correct url for that location. Mine would be ‘http://localhost/workspace/my_project/admin/’
- Change $config['index_page']=”; – The reason for this is that we will be using .htaccess file to remove the index.php from the urls
- Next step is to edit the index.php file in the admin directory (the file you created earlier by copying the index.php from the root) and change $system_folder = “../@codeigniter”;
- Now you’ve completed all these steps if you can run your local server and point your browser to project folder url - i.e. http://localhost/workspace/my_project/admin you should with a bit of luck get the welcome page again. So far so good.. Let’s move on !
CodeIgniter .htaccess Setup
One thing which I dislike about the Codeigniter is the fact that by default the urls all begin with \index.php\ so I usually setup an .htaccess file to remove these.
To do this create two new files called .htaccess. One should be in the root of your project folder and the other should be in your admin folder.
I usually setup my .htaccess file in the root folder to be based on Elliot Haughin’s htaccess template from the Codeigniter Wiki as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#’system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^@codeigniter.*
RewriteRule ^(.*)$ /workspace/my_project/index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /workspace/my_project/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
Once you have saved this to your root folder direct your browser to ‘http://localhost/workspace/my_project/welcome’
If you can view the welcome page your .htaccess redirect is working fine.
For the admin section .htaccess file you need to do something almost identical with the exception of the ReWriteRule URL which should be:
RewriteRule ^(.*)$ /workspace/my_project/admin/index.php?/$1 [L]
Again navigate your browser this time to ‘http://localhost/workspace/my_project/admin/welcome’ and if you can see the welcome page you’ve pretty much cracked it.
SimpleTest Unit Testing Framework
The final thing I would recommend and I must confess I haven’t had a great deal of experience with unit testing yet but it is something which interests me greatly so I always recommend to setup the unit test framework within your project from the outset so that you can build your project using the test-driven development methodology.
The majority of the credit for this setup must go to Jamie Rumbelow. The setup steps are as follows:
- Download the SimpleTest framework and extract the files into your @codeigniter directory.
- In both your main folder and your admin/application folder create a new folder called tests.
- Within the new tests folder setup additional folders called ‘models’, ‘views’, ‘controllers’, ‘libraries’ and ‘helpers’.
- Download my and custom_test_gui text file and rename it custom_test_gui.php file and put this in your @codeigniter/simpletest folder (file adapted from Jamie Rumbelow’s original script).
- Download my test text file and rename it test.php and place it in the root of your project folder and place another copy in the root of your admin folder (file adapted from Jamie Rumbelow’s original script).
- If you now point your browser to ‘http://localhost/workspace/my_project/test’ you should hopefully see the following screen:

- For the test.php script which you copied to the admin directory you will need to Configuration information at the top of the file to the following:
define('ROOT', dirname(__file__).'/');define('SIMPLETEST', str_replace('\admin', '', ROOT).'@codeigniter/simpletest/');define('APPLICATION', ROOT.'application/');define('TESTS', ROOT.'application/tests/');define('APPINDEX', ROOT.'index.php'); - Again if you point your browser this time to ‘http://localhost/workspace/my_project/admin/test’ you should hopefully see the test suite screen shown above.
That pretty much covers the initial setup of the Unit Test framework using SimpleTest. I won’t go into the detail of how to setup your test scripts just now. I do plan to cover some of my methods for this in later articles so subscribe to the RSS feed or follow me on Twitter. If this is something you are interested in I would definitely check out Jamie’s article in probably have a good look at the documentation on the SimpleTest website.
Finalising the Setup
The only remaining things I always do to make sure I have my project setup correctly is to make sure I have the following folders in place.
- In the root directory create a folder called ‘css’ to store your cascading stylesheets.
- In the root directory create a folder called ‘js’ to store your javascript files.
- In the root directory create a folder called ‘images’ or ‘media’ to store your images and documents.
- Similiarly in the admin folder setup the same folders.
- Hopefully with this folder you can make sure that you have a consistent approach to developing your applications.
Hopefully this has given a good insight into my Codeigniter project setup and I’m hoping you find it useful. If you have any questions, suggestions or feedback please leave a comment and I’ll make sure I respond.
If it’s any use and make things easier I’ve also uploaded the whole my_project setup as zip file so you can download it and check it out yourself.







Great article… I’m actually constructing a very similar setup for a project of mine. One question, though… Do you have any examples of your controller tests? I’m asking because in my model tests, I can do something like this:
$this->ci->load->model(‘My_model’);
Then, I can reference the model I’m testing adequately as:
$this->ci->My_model
However, I can’t figure out how to do the same thing with a controller. Any thoughts?
Thanks in advance,
Jazz
Jazz,
Unfortunately CodeIgniter does not allow you to call a controller from within another controller so there is no real way to unit test controllers without hacking the system. The general consensus is that you should not need to test your controllers because they should only be manipulating data to subsequently call your models and views but I know in the real world that sometimes there are things within your controllers that you would want to unit test.
For me I use the Simple Test web_tester functionality to load the page and run checks on the page to make sure that certain data is on the page as expected mainly using the assertPattern($pattern) test. This way you can validate to some extent that your controllers are working as expected. I hope this helps.
Everything seems to work for me until I try the .htaccess part. I’m running XAMPP on Windows and am using an alias directory to point to the applications directory. In other words: “http://localhost/app_name/ ” points to the equivalent “my_project” in your configuration. So I reworked the .htaccess file to read:
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#’system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^@codeigniter.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
But every time I try to go to the “welcome” controller I get redirected to the http.conf root of the server. Any suggestions on what I should be looking for?
Hi John,
I think if you change your RewriteRule lines to the following it should work:
RewriteRule ^(.*)$ /app_name/index.php?/$1 [L]Give that a go and let me know how you get on.
Thanks
VERY nice setup, thanks. You cover all the bases I can think of. I like that single unit test option.
About testing controllers using a controller…. I’ve found that there’s a library that apparently allows you to load controllers into another controller. So maybe that would be what’s needed to test controllers.
http://codeigniter.com/wiki/Wick/
Haven’t tried it yet, as I haven’t set up Simpletest to work with CodeIgniter.
One other thought… when separating the backend and frontend applications, doesn’t that lead to a duplication of models and perhaps some view elements?
Separating the two seems almost like coding two separate websites with separate data and zero shared code base except for CodeIgniter itself.