Sunday 10 July 2011

Developing the first application with Code Igniter

As I mentioned in the introduction, Code Igniter uses the Model-View-Controller pattern to build web applications. This comes in handy for separating business and application logic from the visual presentation. Thus, since I plan to develop a simple application that will display a simple message on the screen, the first thing that I’m going to do is define the corresponding controller class.
To do this, I’ll create a class called “HelloWorld,” which will be saved to the /system/application/controllers/ folder as “helloworld.php." The signature of this sample class looks like this:

<?php
class HelloWorld extends Controller{
function HelloWorld(){
// load controller parent
parent::Controller();
}
function index(){
$data['title']='My first application created with Code Igniter';
$data['message']='Hello world!';
// load 'helloworld' view
$this->load->view('helloworld',$data);
}
}
?>

Now that you know how the “HelloWorld” controller class looks, it’s necessary to dissect it in different parts and explain the functionality. In the first place, you should notice that each time a new controller is created, it must extend the default controller provided by Code Igniter. Therefore, this sample class is defined as a subclass of this controller.
Then, this sample class loads the parent’s constructor and then defines a brand new method called “index().” This method has a special meaning for Code Igniter, since it will be called automatically whenever it finds it was implemented by a specific controller.
Still with me? Great! Then, it’s time to analyze what this method actually does. As you can see, it defines the data that will be passed in to the view file to be displayed later on. This data is first stored on a simple $data array, and second transferred to a view template, called “helloworld.php.”
Each time a view file needs to be loaded, Code Igniter uses a “Loader” class, which is included by default by each new controller created. So, in this case the below expression:

$this->load->view('helloworld',$data);

simply loads the corresponding view file and passes the data that will be printed on the browser to it. Pretty intuitive, right?
Now that you've hopefully grasped how the previous “HelloWorld” controller class works, I’m sure that you’ll want to see how the respective “helloworld.php” view file looks. Below I included the definition of the file in question, which should be saved to the /system/application/views/ folder:

<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $message?></h1>
</body>
</html>

As shown above, the structure of the “helloworld.php” view file is extremely simple. It only contains a couple of “echo” PHP statements, which are used to display the data passed by the controller. Also, you should recall that this data was stored on a $data array, so its respective keys are automatically turned into variables within the view template.
So far, so good. At this point, you hopefully understand how the controller passes certain data in the form of an array to a view file to be echoed to the browser. However, it’s possible that you’re wondering how to test the previous “HelloWorld” controller with Code Igniter.
Code Igniter uses a special routing process that permits you to invoke a specific controller by using an URL. The first URL segment is the name of the controller that will be loaded by a certain application, then the second segment corresponds to the name of a method of the controller that will be invoked. The third segment is an optional argument taken by this method.
This explanation might sound pretty confusing to you. However, the following code sample should dissipate any possible doubts you might have. To call the previous “HelloWord” controller, I’d type the following URL:

http://localhost/codeigniter/index.php/helloworld/

As you can see, the first URL segment calls the “HelloWorld” controller, and automatically executes its “index()” method, which displays the view file previously created. Try it for yourself and you’ll see the classic “Hello World” message printed on your screen.
In addition, to clarify how Code Igniter’s routing process works, say that I changed the definition of the “HelloWorld” controller class and now it looks like this:

class HelloWorld extends Controller{
function HelloWorld(){
// load controller parent
parent::Controller();
}
function display(){
$data['title']='My first application created with Code Igniter';
$data['message']='Hello world!';
// load 'helloworld' view
$this->load->view('helloworld',$data);
}
}

In this case, the URL that should be used to call the “display()” method of the controller would be:

http://localhost/codeigniter/index.php/helloworld/display/

Pretty easy to grasp, right? By using this routing method, Code Igniter assures that all of the URLs generated by a PHP application will look clear and be search engine-friendly as well.
And with this final hands-on example, I’m finishing this first tutorial of this series. As you saw before, I provided you with concrete material that hopefully will help you start mastering the basic features of Code Igniter. You have a lot of fun ahead!
Final thoughts
In this first chapter of the series, I walked you through the core concepts that surround the use of the Code Igniter PHP framework. You hopefully learned how to correctly setup its configuration files, and how to create a simple web application comprised of a single controller and a view file.
I’m only scratching the surface when it comes to discussing the vast set of handy features that come bundled with Code Igniter. Therefore, in the next article, I’ll be covering the use of models and the utilization of some of its core classes.

No comments:

Post a Comment