Getting started with Code Igniter

Posted on March 29, 2009

What is Code Igniter?

Code Igniter is a PHP web framework which assists in creating complex websites. Unlike other web frameworks, Code Igniter doesn’t force you to use all of it’s components. You select and load them as you require. The only feature you have to use is the URL structuring (/controller/method/) which is very helpful anyway.

Code Igniter follows the Model, View, Controller (MVC) principle. All of your database queries are stored in models, HTML/XML output in Views and the logic code in Controllers. You aren’t forced to do this, however it does keep your code organized and reusable.

Helpers and Libraries

Like most frameworks, Code Igniter comes with many classes that help assist development. They are split into Libraries (AKA Classes) and Helpers. Libraries contain code that you could use on every page of your application – Database ORM, Image Manipulation, Pagination and Benchmarking. Helpers are things like Relative URLs, Forms and Email.

Using helpers and libraries is very easy. If you want it included throughout the site see below (autoloads). If you want it included for every method in a Controller, then you can add it to that Controller’s constructor. If you just want it included in one method, add the code inside the method.

$this->load->helper('helper_name');
$this->load->library('library_name');

Installation

Installing Code Igniter is alot easier than installing other PHP web frameworks. There is no need for command line access, nor the need to write your database schema in XML. You only need to edit a few configuration files.

Download and Install the latest version off the Code Igniter download page. Extract it and upload it to your web server. You may notice the directory /user_guide/ . This only contains documentation so it can be deleted safely. Up to date documentation can always be found at www.codeigniter.com/user_guide/.

Now that Code Igniter has been ‘installed’ we need to configure our settings. Open..

/system/application/config/config.php

On line 14 we need to set the path to our website. Make sure it has a trailing slash.

$config['base_url']	= "http://www.website.com/path/to/codeigniter/";

I also put global XSS filtering on. By default it is not active. Replace line 267 with..

$config['global_xss_filtering'] = TRUE;

If your application uses a database, now is a good time to set it up. Open..

/system/application/config/database.php

$db['default']['hostname'] = "localhost"; //usually localhost
$db['default']['username'] = "usernamet";  //database username
$db['default']['password'] = "password";  //password to user set above
$db['default']['database'] = "database";  //database name
$db['default']['dbdriver'] = "mysql";  //database driver - mysql/postgre/sqlite etc..
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

When you call the database class, Code Igniter will connect to the database with the information you set in $db['default'] array.

$this->load->database();

You can then interact with the database through

$this->db->method();

If your application uses more than one database, you can set another array in the same database.php file but call it a seperate name. eg $db['remote'] (copy and paste the $db['default'] array and change where necessary). You can then connect that database with..

$remote_db = $this->load->database('remote', TRUE);

And execute database methods through..

$remote_db->method();

I also recommend you set up autoloads..

/system/application/config/autoload.php

Here you can specify classes and helpers that will be available in every file throughout your project. This is useful because you won’t need to call them every time you make a new controller. I recommend autoloading the database class and the URL helper. You will find the URL helper very useful in your Views, being able to use relative URLs instead of hard coding full http://www.website.com/path/to/codeigniter/ URLs is very useful. You just need to add the name of the library/helper in the appropriate array..

$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');

Thats it.. Feel free to ask any questions.
In the next couple of weeks I’ll be writing a tutorial showing how to get a fully featured authentication system up and running with Code Igniter.

Leave a Reply

You must be logged in to post a comment.

Sourcebits

iPhone Development and Flex Development

Categories