Tutorial: Advanced CodeIgniter Installation

CodeIgniter is a great framework for building web applications but it takes a little work to get it running out of the box. Editing config files and database settings is standard for any framework, and CodeIgniter is no different, but with a few extensions of the core libraries you can quickly and easily build much more powerful tools.

This tutorial assumes you are familiar with MVC frameworks and/or CodeIgniter. If not, you should read this first.

Getting Started

Download CodeIgniter 2.0

CodeIgniter can be downloaded one of two ways:

  1. Get the current release from the CodeIgniter website.
  2. Get the lastest version of CI2 from BitBucket

The contents should look something like this:


Code Overview

  • application - This is where all of your code is going to go. In 1.7.2 this folder is inside the system folder.
  • index.php - This runs CodeIgniter. You'll have to edit a couple of things in here.
  • license.txt - blah, blah, blah.
  • system - CodeIngiter core. DO NOT edit these files. Upgrading is much, much easier if you don't. CI provides core extensions which we will cover later.
  • user_guide - HTML documentation for CI libraries.


For security and sanity, it's a good idea to rename and move the system and application folders. It's not required but it makes organizing multiple applications much easier and it allows you to use one copy of the CI core classes across multiple applications.

I generally move both the system and application folder outside of the web server document root, rename the system folder to codeigniter and rename the application folder to the name of the application I'm working on. From here on out we'll just refer to the application folder as 'application.'


If we have a site http://www.example.com and its document root /var/www/example.com/html/, then we can set up our app like this:


Now we can start running CodeIgniter.


First thing's first. We need to tell the bootstrapper where to look for CodeIgniter on the server. Basically, we are going to set $system_folder and $application_folder to the direcotries where we saved the system and application folders.


Open /var/www/example.com/html/index.php and modify it like so:

/* *--------------------------------------------------------------- * SYSTEM FOLDER NAME *--------------------------------------------------------------- * * This variable must contain the name of your "system" folder. * Include the path if the folder is not in the same directory * as this file. * */ $system_path = "/var/www/example.com/codeigniter"; /* *--------------------------------------------------------------- * APPLICATION FOLDER NAME *--------------------------------------------------------------- * * If you want this front controller to use a different "application" * folder then the default one you can set its name here. The folder * can also be renamed or relocated anywhere on your server. If * you do, use a full server path. For more info please see the user guide: * http://codeigniter.com/user_guide/general/managing_apps.html * * NO TRAILING SLASH! * */ $application_folder = "/var/www/example.com/application";

Once that is done, we need to set a few configuration options.


You aren't required to set any config values but it's a good idea, especially if you want database connectivity.

Main Configuration

Open application/config/config.php. Set the base url to http://example.com and the index page to nothing. Normally this would remain index.php but we are going to remove index.php from the urls via an htaccess file.

$config['base_url'] = 'http://example.com'; $config['index_page'] = '';

Database Settings

Next set your database settings in application/config/database.php. NOTE: Your database must already be created. CI will NOT create your database.

Cleaner URLs

Now we can clean up the urls by rewriting them without index.php. This is most easily done with an htacces file. Create or open the file /var/www/example.com/html/.htaccess and add the following:

RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L]

Now, if you point your browser to http://example.com you should see the CodeIgniter welcome page.

Going Deeper

Extending the Core

Another good thing about CodeIgniter is the ability to extend the core libraries.

By default, all controllers have to extend the CI_Controller class, but what if you want all of your controller constructors to run a shared piece of code, like checking to see if a user is logged in? You could write a helper function and call it from every constructor, but that's not very efficient, or coder-friendly. Enter CodeIgniter core extensions.

To extend a core library, created a file called MY_library_name in the application/core/ where library_name is the name of the core library.


In this example we will create a class MY_Controller that extends CI_Controller and then create a new controller that extends MY_Controller.

First, we create MY_Controller.php in application/core.

<?php class MY_Controller extends CI_Controller { function __construct() { parent::__construct(); // your code here } }

Now we can create controllers like normal, except now we make our controllers extend MY_Controller.

<?php class Blog extends MY_Controller { function __construct() { parent::__construct(); // some code } function some_function() { } }

Now you can perform tasks in MY_Controller that all future controllers will share. This same process can be used to extend most of the core classes and libraries.

Where do I save my extended libraries?

If you are extending a class in system/core it goes in application/core and if extending a class in system/libraries it goes in application/libraries.

But what about the views?


By default, CodeIgniter comes with a simple template parser but it's not required, for performance reasons, and it's just a parser so it doesn't really provide much functionality.

There exist, however, a number of template libraries that allow to easily load view files into template regions and therefore reduce code reuse.

I prefer Phil Sturgeon's Template library. It's easy to use and it supports the use of "themes" if you're looking for that kind of functionality.

When used in conjunction with CodeIgniter's template parser, or even better Dwoo, you can create a simple, elegant templating system.


The last tip I have is for you to use an asset package. By assets I mean javascript, css and images. It can be tedious to retype or copy '

The files we need are:

  • application/config/asset.php
  • application/libraries/Asset.php
  • application/helpers/asset_helper.php

If you haven't already, create an assets directory with css, js and img directories.



Create a file welcome.css and save it in assets/css/. To load this css file in a view:

<?php echo css('welcome.css'); ?>

Which will print '<link rel="stylesheet" type="text/css" href="/assets/css/welcome.css" />

You can do a lot more, but this should be a good starting point. Feel free to download the source and play around.

Download: codeigniter-custom.zip