Getting Started with CodeIgniter

CodeIgniter gives you the ability to write PHP code quickly and effectively while Twitter Bootstrap allows you to create interfaces quickly without needing to style primary elements. However, the two are not combined and certain things between CodeIgniter and Twitter Boostrap do not play nicely. With CodeIgniter Bootstrap, it allows you to skip this “combination” phase and have you start developing your idea instead of developing the common web elements.

Getting Started

Of course the first step is to download the codeigniter framework. You can download the latest stable version of codeigniter from the following url http://codeigniter.com/downloads/ . It will be a compressed file (zip file) whcih will be downloaded, just unzip it and keep it in a folder, say codeigniter. Make sure you keep it inside the htdocs or webserver’s directory.

Not its time to configure the codeigniter which initial settings to start working on. To do so, open the file config.php in to your favorite editor. The file resides inside system/application/config directory. You will see an array called $config which stores all the configuration information, we will change only the following values and leave the rest as it is.

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
*/
//don't forget the TRAILING slash (/)
$config['base_url']	= "http://localhost/codeigniter/";

Trust me, that shall be all to get you started. Some blogs/sites have show much changes in this file, but you can play around with them when needed. You can change a lot about your application form this single file. Like you can store the session information inside a database, by simple setting up the config var $config[‘sess_use_database’] as TRUE, which is FALSE by default. You can also change the name of the session table from default “ci_sessions” to anything you like.

Since none of the web application runs without database, so let me show you how to configure database in this setup. I decided to pick this up in next tutorial, but since its pretty straight forward, let’s do it. To configure database, open database.php which is located in the same directory as of config.php i.e. system/application/config . Now change the values of the following array keys with the actual values of your MySQL server.

$active_group = "default";
$active_record = TRUE;
 
$db['default']['hostname'] = "localhost";
$db['default']['username'] = ""; //database username
$db['default']['password'] = ""; //database password
$db['default']['database'] = ""; //name of the database
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = ""; //any table prefix, if you want to keep one
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

As you see the database configuration was also simple. Now its time to develop a simple application using the CI framework. At the very beginning of this post I mentioned that CodeIgniter gives you a MVC pattern structure, so every part has a separate folder. Let us create a simple controller class to start with.

To do this, lets create a class called Hello which will be written in the file called hello.php, stored inside system/application/controllers/ folder. The content of the file can be : (you can have different class, after all its just a PHP class).

class hello extends Controller
{
  function  __construct()
  {
    //call parent constructor
    parent::__construct();
  }
 
  function index()
  {
    $output['header'] = "My page's header";
    $output['content'] = "Here is the content of the webpage";
    //load from from the system/application/views/hello.php
    $this->load->view('hello',$output);
  }
}

Now let us understand the code line by line. Its a controller class called hello which extends the default parent class called Controller. Extending the Controller class facilitates the execution of this controller. The constructor of this calls simply calls loads the constructor of the parent class, which apparently is Controller. Next is the index(); function which has a special meaning in CI framework. If its present in a controller, it is executed automatically when the controller is loaded/executed.

Now we output the data using the view part of the MVC pattern. if you notice the above code loads a view called hello which is a file kept inside system/application/views/ folder. The file name should be same as the name of the controller. And the data is provided in the form of an array, where header,content  are the template variables, as show below in the hello.php

<html>
  <head>
    <title>My first CodeIgniter Controller and View</title>
  </head>
  <body>
    <h1><?php echo $header ?></h1>
    <p><?php echo $content ?></p>
  </body>
</html>

The above view renders the data which is passed on by the calling controller. Now lets try and test if everything works as we expected, phew ! Open your browser and hit

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

If everything is well, you should see a webpage, with a header and body content as specified in the controller. Wonder, how did that got render ? CodeIgniter provides a very nice routing mechanism based on REST. The URL is so that you can call a specific controller and a method from inside the controller. But if you notice, in the above URL we have mentioned only the controller name i.e. hello and not the function name index(). As mentioned previously, This is due to the fact that index();function gets called automatically if it exists. Might it have been another function called render then the above URL would have been changed to

http://localhost/codeigniter/index.php/hello/render/

The above URL which now call the render(); function from the hello controller.

CodeIgniter was easy, no ? If you think that was tough, just go through the steps again and I am sure you will understand. Since CI is the most easiest framework and is lets you get started very quickly with the development.

Leave a Reply

Your email address will not be published. Required fields are marked *