An introduction to codeigniter

Codeigniter is my MVC framework of choice when working with PHP as it is straightforward and lightweight. MVC stands for Model View Controller which is a software development method that takes revolves around separating the data layer (the model), the business logic (controller) and the layout (the view). The idea behind this is that the software produced is easier to maintain and more robust than with other types of development, I think this is particularly prevalent in PHP where the ‘code behind’ model or inline PHP is often seen.

I particularly like the way that Codeigniter handles forms and validation and it allows the developer to keep the code clean and free from clutter.

Getting Started
To get started in Codeigniter head over to: http://www.codeigniter.com/ and download the latest version. Install this on your development server and you’re ready to get started.

Codeigniter Anatomy
The installed folder contains a system folder, an application folder and some other bits that we don’t really need to worry about. What we’re interested in is the application folder.

Initially there is a single controller in the controller folder with a single function: index() This function loads the ‘welcome_message’ view. Each public function inside of a controller generally is used to represent a page or response to a HTTP request. Other functions may be added but should generally be marked as private or protected to prevent them from being invoked by site users.

It is important to note here that convention is very important in MVC. A public function in a controller can be invoked via a HTTP request. Consider the following controller:

class Welcome extends CI_Controller {
     public function index(){
	 $this->load->view('welcome_message');
     }

     public function show(){
            $this->load->view('welcome_message_2');
     }
}

In this instance the show function can be executed using the following request:
www.test.com/index.php/Welcome/show

and the index function can be executed using the following request:
www.test.com/index.php/Welcome/index

however, the index function can also be executed using the following request
www.test.com/index.php/Welcome/

This is because the default function for a controller is the index function. A standard url in Codeigniter will consist of a Controller/Function/Variables

The Model
The default installation in Codeigniter does not come with a model installed however this is where the data will be loaded and manipulated, this will more often than not involve database interactions.

The View

 $this->load->view('welcome_message');

This function loads the view named ‘welcome_message.php’ in the view folder of the application and displays it to the end user.

Routes
In the config folder is a file called routes.php. This is an important file in Codeigniter as it allows you to map your URI requests to specific controllers and classes. The initial install only contains the following route :

$route['default_controller'] = "welcome";

If for some inexplicable reason we wanted to create a URI for our previous show function in the Welcome controller via the following url : www.test.com/index.php/yo/ we could create the following route :

$route['yo'] = "welcome/show";

Which maps our request onto the appropriate Controller and method.

That’s all for now, in the next post I will cover the basics of creating a Codeigniter Application.

Leave a Reply