CodeIgniter Setup – Part 1

Here I will explain how I setup CodeIgniter along with creating Base models and Contollers
in Part 2 and Part 3

Full working example of all 3 parts available on GitHub:

If you only want the code for Part 1 you can pull Stage 1 from the repo.

Moving core files outside of the webroot and general setup

delete www/user_guide
move www/application to core/application
move www/system to core/system
create www/.htaccess to sort out redirects

The user_guide is of no use unless you want to keep it for examples on how to use CodeIgniter
You have now moved application and system folders outside of the web root so all coding is not accessible without FTP or SSH access to your server

Create structure for general use
create www/robots.txt
create www/assets/css
create www/assets/images
create www/assets/js
create www/uploads

The robots.txt is for SEO
The assets folders are for your css stylesheets, images and js
The uploads are for all resources you want to be available directly on your site (usually controlled in your CMS – Content Management System)

create .gitignore


This stops the uploads folder being committed, any files in the .settings folder (if you use Aptana as your code editor) and any cache files should you use caching later.
The example on GitHub goes allot further removing .zip .dll and anything else that you may not need.

edit www/index.php

if ($_SERVER['HTTP_HOST'] == 'githubci.sdev') {
define('ENVIRONMENT', 'development');
} elseif ($_SERVER['HTTP_HOST'] == ''){
define('ENVIRONMENT', 'testing');
} elseif ($_SERVER['HTTP_HOST'] == ''){
define('ENVIRONMENT', 'staging');
} else {
define('ENVIRONMENT', 'production');

This shows an if statement for HTTP_HOST to jump between environments depending on which domain you are on.
Obviously this will the domains you have setup your site for.

Also change system_path and application_folder to match the locations we have now moved these folder to:
$system_path = '../core/system';
$application_folder = '../core/application';

edit core/application/config/database.php
$active_group = ENVIRONMENT;

This now allows you to hook into certain databases depending on which domain you are running.

I find this useful as I never have the same access/database details with the staging and live sites.
Obviously you need to setup your database access details below if you are connecting to a database.

Setting up the profiler on your development and staging servers (not on live)

The profiler is a great tool when developing your website and helps identify if there are any problems.
By setting up your code to always show the profiler when your not on the production site will help during development.

edit core/application/config/hooks.php

if (ENVIRONMENT != 'production'){ // show profiler for debugging
$hook['post_controller_constructor'] = array(
'class'    => '',
'function' => 'enable_profiler',
'filename' => 'profiler.php',
'filepath' => 'hooks',
'params'   => array()

This sets up the hook to call profiler.php if you are not on the production site.

create core/application/hooks/profiler.php

function enable_profiler()
$CI =& get_instance();

This is to show the profiler if this file is called

edit core/application/config/config.php
$config['enable_hooks'] = TRUE;
$config['index_page'] = '';
$config['time_reference'] = 'gmt';

enable_hooks allows the profiler to show on all sites except the production
index_page is no longer needed as we use URL rewrites
time_reference set to GMT just makes life easier for me when running time based queries – not required

So this is the inital setup.
Part 2 will show you how to create Base Contollers.

1 comment for “CodeIgniter Setup – Part 1

  1. January 15, 2012 at 11:19 pm

    I also think it’s best to move the webroot out of any system folders. So if you want a designer to make basic CSS amends, they only have access to what they need.

    I expect you’ll cover database connection configuration in the next part. CodeIgniter is also great because each developer or member of the project can override any pre-established config with their own verion;) Nice.

Leave a Reply

Your email address will not be published. Required fields are marked *