Getting Started With CodeIgniter – 2

Getting The Environment Set Up

Last time, we installed the software and altered the config.php file by adding two lines (one commented out). Please review Getting Started With CodeIgniter – 1 for details.

If you now visit the home page running locally on your PC, you should see this. Obviously you must substitute your root directory where I have used chaingang3.

Setting up database.php

Most web sites demanding the attentions of CodeIgniter will need a database. I am using MySQL. I created a MySQL database called chaingang3 and set up a username and password for it.

To communicate this to the CI system, edit the database.php file located in the config directory (same place as the config.php file was located).

Add the following lines to database.php, but of course use your own username, password and database name.  The placeholders for these parameters are already in the file, you just have to fill in the values.

$db[‘default’][‘hostname’] = "localhost";
$db[‘default’][‘username’] = "myusername";
$db[‘default’][‘password’] = "mypassword";
$db[‘default’][‘database’] = "chaingang3";

Setting Up constants.php

I like to place any constants that I need to have available throughout the web site in the constants.php file. So early in your development you won’t know what these are for the mostpart, but each time you need a new global constant, set it up in here. To start with I will put in the Version number of the site I am developing. This is the site’s 3rd version so I’ll make a suitable constant, and add it to constants.php.

define(‘WEBSITE_VERSION’, ‘3.0’);


Setting Up Custom Functions

As you code, you’ll need to write PHP functions.  There is a special place to put these in CI. Go to system/application/helpers and create a custom helper PHP file with a name like mycustomfunctions_helpers.php.

For example, one function I always add to my custom helper file is something to alert PHP values without having to write the Javascript each time. This is symptomatic of my never using a debugger.  Here it is :

As time goes on, you’ll add more of your own functions to this file.

Sub Directories for Image, CSS, Javascript and Other Files

I put all the images that are part of the web design into a sub directory called site-images and all content images into a sub-directory called images. Further, I like to place all my CSS files into a css sub-directory and Javascript files into a sub-directory called scripts. I follow the same regime for any other special files the web site may need  – like say for example a sub-directory for flash movies, another for PDFs and so on.

To do this, create all your required sub-directories under the CI application directory in a new sub-directory called assets.  Of course you don’t have to call it assets – I do because someone on the CI forums suggested the name. Anyway – I set the following structure up and so I’ll always have somewhere specific to place required files.