Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Rapid Application Development with CodeIgniter

By Akash Mehta
2008-02-17


Introducing CodeIgniter!

Before we delve into the internals of CodeIgniter, let's have a look at a quick sample of how it works. Grab a copy of CodeIgniter from CodeIgniter; I'll be working with version 1.6.1 in this example.

Let's say we have a very basic application that takes a name via the URL and writes out, "Hello $name!". Here's how we would build it in just PHP, and then in PHP with CodeIgniter.

Raw PHP

index.php:
<?php
$name = $_GET['name'];
if (!$name) die('No name supplied.');

echo '<html>';
echo '<body>';

echo 'Hello, '.$name.'!';

echo '</body>';
echo '</html>';
?>

Not exactly the best PHP script ever, but it should do for our demonstration.

CodeIgniter

CodeIgniter enforces consistent and effective file seperation from the ground up, with a simple filesystem structure as part of their implementation of MVC. The system/application folder has subfolders called 'controllers', 'models' and 'views'.

system/application/controllers/hello.php
<?php
class Hello extends Controller {
function index($name) {
$data['name'] = $name;
$this->load->view('show_name', $data);
}
}
system/application/views/show_name.php
<html>
<body>
Hello, <?=$name?>!
</body>
</html>

Here you can see we have two seperate files, which, as you might have guessed, are the controller and view. The controller is entirely OOP; the index() method is called automatically when the user visits. The view file is almost entirely HTML, using alternative syntax to display the $name variable, which is drawn from the 'name' element of $data passed to $this->load->view(). Every element of $data is made available to the view as a variable of the same name. In this way, you can consistently pass data between your controller and view.

Notice that the view looks much cleaner than the echo calls we used earlier. Sure, you could jump in and out of PHP for that small example; maybe you only need one line of code to achieve the same functionality. Now, if your application is that simple and you aren't worried about XSS vulnerabilities, it's okay. However, our CodeIgniter version is better prepared to increase in complexity, with clean seperation between the business logic and the presentation. When we extend this in future, we can easily modify the frontend or the backend (or both) while making it easy to keep track of everything.

Anyway, in this example, our controller class Hello is extending the CodeIgniter class Controller giving it access to the framework core. This includes functionality for input filtering, the library for which is loaded at $this->input. By passing an optional parameter to $this->load->view(), we provide it with data to make available to our view. Our view then displays this data.

If I point my browser to http://localhost/ci/index.php/hello/show_name/Akash/, sure enough, I see the text "Hello, Akash!" on the screen. Now, the URL isn't pretty. With a simple .htaccess file (the directives for which are available in the CodeIgniter manual), we can remove the index.php. With a quick routing hack we can also remove the hello. Finally, we can rename index to something more appropriate - how about show_name? With a few quick changes, I now have a CodeIgniter-based MVC web application (albeit one lacking in any useful functionality) available via http://localhost/ci/show_name/Akash.

Just a note on URLs: when you drop CodeIgniter into your website, it takes over as the application entry point (from its root installation directory, e.g. ~/public_html/ci) . From now on, your URLs will look something like http://example.com/index.php/hello/show_name/Akash. In this example, CodeIgniter takes apart the URL and routes it accordingly, working out that Hello is the name of the class (note: class names start with a capital), it should call the show_name() method within the object of that class, and pass the value 'Akash' as the first parameter to the method. You can have as many parameters as you like, just adding them on the end, seperated with /'s. Therefore, your URLs look something like http://example.com/index.php/controller/method/param1/param2/param3. The index.php can easily be taken out using .htaccess files and mod_rewrite. More on this in the manual.

Notice the framework also handles clean search-engine-friendly URLs for us. Oh, and the glaring XSS vulnerability in our pure-PHP example earlier? Try putting anything vaguely harmful into the URL and CodeIgniter will reject it. It's XSS-proof already, with not a single input validation routine in sight. We've barely started and already CodeIgniter is making our life easier.



Tutorial Pages:
» Why should you be using CodeIgniter?
» MVC 101
» Introducing CodeIgniter!
» Getting our feet wet
» Further CodeIgniter


Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1