20 Oct 2010 - Posted in PHP

Introduction to CodeIgniter

Preview
CodeIgniter is a PHP framework, it’s basically a library of code that you can use to build your own project on top of. Some of the advantages of using CodeIgniter are that it provides you with a really easy way to organize your code in a logical manner. It features a lot of readymade classes to make the laborious tasks, like form validation, simpler and its really well supported, with a great user guide and active community.

Codeigniter is based on the model-view-controller pattern which divides a system into layers. The view layer is what the user will see and interact with; it’s typically an HTML page.

The model layer is used to interact with a data source, typically a database. It handles all the SQL queries.

The controller layer goes in between these layers and receives input, performs actions on data, and interacts with the model and view layers.

In this tutorial, I’m going to explain how to get started with CodeIgniter, what set-up and configuration you have to do, and demonstrate how you would go about building a simple application.

Step 1: Getting started

The first thing you’ll need to do is download CodeIgniter. After that, you can start by creating a controller which will need to be placed in the controller directory. The code for a basic controller looks like this:

<?php
class Main extends Controller {

}
?>

In this case I’ve called it Main, this means the file would need to be saved as main.php as the class name should be the same as the filename only with the first letter capitalized.

Similarly, you can create a model in the same way. Just extend model and save it in the model directory.

<?php
class Main_model extends Model {

}
?>

Step 2: Configuration

There is a small amount of configuration to be done, starting with the database. You’ll need to set your default database, along with the connection details, in the database file in the config directory. Once you’ve done this, it’s also a good idea to set a default controller. This can be set in the routes file, also in the config directory. In this case you would set the default controller to main.

Step 3: The Model

The model layer is responsible for the interactions with the database. An example method in a model class could be:

function select_id($params) {
	$query = $this->db->query("SELECT name, email FROM example WHERE ID = '" . $params['id'] . "'");
	return $query->result_array();
}

Step 4: The Controller

Functions in a controller are accessed when the method name is defined in the URL structure. For example, the URL http://example.com/index.php/controller/method if the method is named index then this will be called by default. It is also possible to set a constructor method which will be called whenever that controller is accessed, as well as a standard method.

Model classes need to be loaded by a controller before that controller can access that model class’s methods. This is often done in the constructor.

function Main() {
	parent::Controller(); //this line is essential for a constructor
	$this->load->model('main_model', '', TRUE); //the TRUE value means the DB is connected to
}

After this you could set an index method that calls the method in the model class and then loads a view and passes the results to the view.

function index() {
	$params['id'] = 5;
	$results['selection'] = $this->main_model->select_id($params);
	$this->load->view('view',$results);

}

Step 5: The View

The view will mostly be your HTML structure but saved as a .php in the views directory. Views can be split into headers and footers etc, just load them from the controller in the appropriate order.

In this example, the view has been loaded with a result array which can be accessed and displayed using short hand PHP. For example:

<ul>
<?php foreach($selection as $result):?>
<li><?=$result['name']?></li>
<li><?=$result['email']?></li>
<?php endforeach;?>
</ul>

This would loop through the results and display them in a list.

Conclusion

This has been a very basic introduction to getting started with CodeIgniter. To learn more, I recommend reading the CodeIgniter user guide.

Post a Comment

Lifestream