Building a Complete CodeIgniter Application: Part 1

Jim O'Halloran • September 10, 2007

php codeigniter feedignition

Over the coming weeks/months I'm going to write a series of blog posts describing the construction of a complete AJAX application using the CodeIgniter framework. I've chosen to build a multi-user Feed Reader, which I'll call " Feedignition". Feed Readers seem to be the new "hello world", and there's good feed parsing libraries available which allow us to concentrate on the application itself without having to worry about the myriad of details involved in actually parsing of a feed. That leaves us free to explore a number of topics which will be of interest to anyone building applications with CodeIgniter.

I'll assume you're familiar with PHP programming, and have a PHP development environment already set up on your machine, including web server, MySQL database, and so forth.

I'll post the series in parts as I write it, but I won't commit to a schedule for new parts. All code in this series (including the final product) is released under the terms of the GNU GPL.

Lets get started, with part 1!

Basic Configuration

Before we can dive into the application itself we need to get a basic development environment up and running. I'm developing on a Linux machine using Apache 2.2, PHP 5.2, and MySQL 5.0. If you're on Windows the easiest way to get up and running would be to download XAMPP from . Most of the CI video tutorials skip over this bit and start off where this part finishes up, but preparation is very important, so I figure we should start at the beginning.

Create a working directory for your CodeIgniter project. If you're running Linux, the web server root directory will usually be /var/www/html. If you're not using apache for anything else, that can be used as your working directory, otherwise create a folder under there to use. I'm develop this project in a directory called "feedignition" under my document root.

Download the CodeIgniter source from the official site, and unzip it into your working directory, preserving subdirectories.

Now we need to fix the permissions on the logs and cache directories to ensure the web server can create files there. On Linux, you'll want to do the following:

cd /var/www/html/feedignition/system
chown apache:apache logs
chown apache:apache cache

When I'm developing web applications I prefer to keep as much of the application as possible outside the web root. So I want to split the standard CI installation into two bits. I want to create a html folder which will be the document root when we deploy the finished application, and leave the existing " system" folder outside the document root. So I'll develop this app on my local machine in the URL http://localhost/feedignition/html/. The html folder will be web accessible, but virtually empty, while the bulk of the CI code is hidden. Lets create a html folder and put index.html into it: ```

cd /var/www/html/feedignition 
mkdir html 
mv index.html html

We've rearranged CI's default directory structure, so we need to tweak a couple of settings in the html/index/php file. Open it up in your favorite text editor and change the $system_folder line to '$system_folder = "../system";'.

Now we want to clean up the default CI install a bit. The system directory will be outside of the document root when the application is in production, so the index.html files which are in every CI directory are pointless, lets get rid of those. Lets also get rid of the CI license file, the license is available in the manual or online if we need it again. ```

cd /var/www/html/
find . -iname index.html -exec rm {} \;
rm license.txt

You can also get rid of the CI manual at this point if you want. I've got a number of projects I've developed using CI, and I don't want the manual in the development folders for every one of them. If you want to get rid of the manual, use the following commands: ```

cd /var/www/html/feedignition 
rm -rf user_guide

Let's just make sure that we haven't broken anything so far. Open up your web browser and go to http://localhost/feedignition/html (or whatever URL corresponds to your working directory). If you see a CI welcome page, so far, so good!

We'll need a database later on to hold things like users, subscriptions and feed items, so lets create one now and configure it in CI. I like to do mine from the MySQL command line, but you might want to do use phpMyAdmin instead. Create a database, and also create a new user granting all permissions to that user. From the MySQL command line I run the following statements: ```

create database `feedignition`;
grant all on feedignition.* to feedignition@localhost identified by "YOUR_PASSWORD_HERE";
flush privileges;

We should now edit system/application/config/database.php and configure CI with the name of the user and database we just created:

$db['default']['hostname'] = "localhost"; $db['default']['username'] = "
feedignition"; $db['default']['password'] = "YOUR_PASSWORD_HERE";
$db['default']['database'] = "feedignition"; $db['default']['dbdriver'] = "

We'll use the database on pretty much every page load, so lets automatically load the database class so it's available everywhere. We'll also use the URL helper a lot, so lets go ahead and autoload that as well. Edit system/application/config/autoload.php, and look for each of the following autoload lines, and chenge the values as shown below.

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

If you open up your web browser and go to http://localhost/feedignition/html again now, we can test our database connectivity. If you see the welcome page this time the database connection is ok. If you see a database connection error make sure the database configuration is correct.

I want to use mod_rewrite to remove "index.php" from the CodeIgniter URL's, so lets now edit the system/application/config/config.php file to get that underway. Find each of these lines, and change them as appropriate:

$config['base_url'] = "http://localhost/feedignition/html/";
$config['index_page'] = "";

To make mod_rewrite work we'll need a .htaccess file in our html/ folder. Create a file called .htaccess and put the following rewrite rules:

RewriteEngine on
RewriteRule ^$ /feedignition/html/index.php [L]
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /feedignition/html/index.php/$1 [L]

If everything works correctly, you should now be able to point your browser to http://localhost/feedignition/html/welcome/welcome and see the welcome page. If you get a 404 Not Found error your .htaccess file is being ignored, you may need to enable .htaccess by adding the following to your httpd.conf file, then restarting Apache:

<Directory //var/www/html/feedignition/html>
    AllowOverride All

Once you can see the Welcome page at http://localhost/feedignition/html/welcome/welcome we know that the basic framework, database, and mod_rewrite are all configured and working. We no longer need the default welcome message controller and views, so lets delete those now:

cd /var/www/html/feedignition/
rm system/application/controllers/welcome.php
rm system/application/views/welcome_message.php

So there we have it, CI is configured, we've got a database, and mod_rewrite all set up and ready to go, and we're ready to build our application. The framework doesn't do anything at the moment, any URL we try and access will give us some sort of error message.

I follow these basic steps every time I build a CodeIgniter application, once done it gives you a nice clean foundation on which we can build the remainder of our application. Next time we'll start our Feed reader.