Beginning Codeigniter Application Development

Prerequisites:

However, prior using codeigntier, its required that you know what mvc design pattern is. To more about mvc, you can start with the wiki article on mvc .
Also, wish you have already xampp/wamp or something similar already set up in your computer. Now, Download the latest version of codeigniter. extract it and copy the directory to the root of your local server. without doing anything else, you should be able to run this and see a page like as follows from this local url: http://localhost/codeigntier/
Running Codeigniter Firsttime

Directory Structure:

The application directory structure is mainly consists of 2 directory, ‘system’ and ‘application’.
Codeigniter Root Directory Structure
The ‘system’ directory is the core and consists of the core framework files. When a new version is released, you can update your existing application just by replacing this system directory with the latest release. We won’t never going to work on this folder our selves ever. All our codes are going to take place in the ‘application directory’. There you will see a list of sub directories as like the following image:
codeigniter application directory structure
Lets get a little familiar with the directories:
  • Cache : This directory will contain all kinds of cached files if you are using so. However, you will may need to provide write access for the application to this directory as codeigniter will need to create temporary files on this folder. To know how to use caching, please refer to codeigniter caching tutorial.
  • Config : This directory includes settings/configuration related information like database settings, route information, constants declaration, items to be auto loaded etc. To know more, please visit codeigniter documentation about using config class .
  • Controller : This directory includes all controller definitions. It can be said as the entry point of application also, every requests triggers a certain method of a controller.
  • Core : If we need to extend the functionality of any core classes like controller,loader,router etc, then we can declare new class here which will extend those core classes and put the implementation inside those.
  • Errors : This directory includes some basic template for showing various kind of errors like db error, php errors, 404 errors etc, which we can change later as per our requirements.
  • Helpers : This directory will include all helper files. Read more about using helpers.
  • Hooks : This directory will include all hooks declaration. Read more about using hooks
  • Language : This directory will include language files. By loading different language files, we can make our application multilingual supported. Read more about using language class.
  • Libraries : This directory will include all library class files that might need to be created for our applications.
  • Logs : This directory will include all application logs. TO know how to write log for debug/error/info, please more about error handling
  • Migrations : This directory will include migration helpers.
  • Models : This directory will include all model classes used in our application. This section can be said as the ‘data access layer’ of our application.
  • Third_party : This directory will include library files, but only those, which are imported from third-party. So, difference between the ‘third_party’ and ‘libraries’ is that, one is for self-made libraries, for app specific, other one is for importing party libraries.
  • Views : This directory will include all view template files.

Application URL Structure:

Codeigniter uses a very easy and handy url structure scheme. and it is completely inter-related with the controller class name and method name. to access a controller method, you have to use url like {root_url}/index.php/{controller_name}/{method_name}. To know more in details, please refer to the codeigniter url structure documentation .

Create A Basic Controller:

Lets start by making a basic controller class file. Create a new file in the ‘controllers’ directory and name it ‘blog.php’. Here is the code snippet for this controller class:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Blog extends CI_Controller {
 
    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/blog
     *  - or - 
     *  - or - 
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/blog/{method_name}
     */
    public function index()
    {
        echo "Hello World";
    }
}
 
/* End of file Blog.php */
/* Location: ./application/controllers/blog.php */
Now, run the application with the url “http://localhost/ci_app/index.php/blog”, you will see the sentence ‘Hello World’ on the page. ‘index’ the default method that is being executed when no other method is specified in url. And we understood one thing clearly, to create a page, we may ignore to have a model and/or a view file, but we must need a controller.

Create a model :

Lets create a simple model on our application. We won’t cover any database activity on this tutorial. But, its pretty easy. setup your database server info on the ‘config/database.php’. Then you can access database in either straight cut way or use codeigniter provided ‘active record’ library that helps a lot creating/executing SQL query easily. You can read more on my another article about codeigniter active record class. Here is the code of the model class we will be using:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blogmodel extends CI_Model{
 
    /**
     * returns a list of articles
     * @return array 
     */
    function get_articles_list(){
        $list = Array();
         
        $list[0] = (object)NULL;
        $list[0]->title = "first blog title";
        $list[0]->author = "author 1";
 
        $list[0] = (object)NULL;
        $list[1]->title = "second blog title";
        $list[1]->author = "author 2";
 
        return $list;
    }
}
?>

Modify The Controller:

To use the data from model, lets change our controller code that will receive data from model and send it to the view for final rendering. Here is the modified version of the controller:
01
02
03
04
05
06
07
08
09
10
11
12
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Blog extends CI_Controller {
 
    public function index()
    {
            $this->load->model("blogmodel");  
            $articles = $this->blogmodel->get_articles_list();           
            $data["articles"] = $articles;
            $this->load->view('blog/index',$data);
    }
}

Create A View:

Lets create a simple view file that will show the data we get from the ‘blogmodel’ in a basic HTML template. Following is the template code:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Welcome to Blog Page</title>
 
<style type="text/css">
body {
 background-color: #fff;
 margin: 40px;
 font-family: Lucida Grande, Verdana, Sans-serif;
 font-size: 14px;
 color: #4F5155;
}
 
a {
 color: #003399;
 background-color: transparent;
 font-weight: normal;
}
 
h1 {
 color: #444;
 background-color: transparent;
 border-bottom: 1px solid #D0D0D0;
 font-size: 16px;
 font-weight: bold;
 margin: 24px 0 2px 0;
 padding: 5px 0 6px 0;
}
 
code {
 font-family: Monaco, Verdana, Sans-serif;
 font-size: 12px;
 background-color: #f9f9f9;
 border: 1px solid #D0D0D0;
 color: #002166;
 display: block;
 margin: 14px 0 14px 0;
 padding: 12px 10px 12px 10px;
}
 
</style>
</head>
<body>
<h1>Welcome To my blog</h1>
<?php
foreach($articles as $article)
{
?>
<h3><?php echo $article->title; ?></h3>
By <strong><?php echo $article->author; ?></strong>
<?php
}   
?>
</body>
</html>

Calling a view from controller:

The above “Modify The Controller” already demonstrates how to call a view. However there are few things you will need to remember. A traditional codeigniter view is a simple php file with “.php” extension. You can place it anywhere inside the “views” directory. Just, you will have to mention the correct path inside the ‘views’ directory. Like if you have a view inside a “blog” directory(which resides in ‘views’ directory, of course). Then you will have to pass the view parameter as follows:
1
$this->load->view("blog/template_name");
Notice that, we haven’t use any extension(here .php) for the template name as it recognize it automatically.
Another important thing to remember is that, the second parameter is optional to call the view. If you have any data to pass to view, then pass it through the second parameter. If you haven’t, then you won’t have to pass any second parameter at all.
Also, data that are passed to view are parsed from associated array. Lets say, we are assigning and passing some data from controller to view. The controller code is as follows:
01
02
03
04
05
06
07
08
09
10
11
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Test extends CI_Controller {
 
    public function index()
    {          
            $data["name"] = "Md Ali Ahsan Rana";
            $data["designation"] = "Software Engineer";
            $this->load->view('test',$data);
    }
}
The view code is as follows:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
<!-- File name: test.php -->
<!-- Location: application/views/test.php -->
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome to Test Page</title>
</head>
<body>
<h2>My Name is <?php echo $name; ?></h2>
<h2>My Designation is <?php echo $designation; ?></h2>
</body>
</html>
Notice that, every associative key from $data becomes as a variable on the view section. So, any data variable you may need on your view, you will have to wrap it with a common variable’s associative index(here $data variable) and finally pass this wrapper variable. Codeigniter will parse it automatically to its corresponding component variables.

References:

As long as you know the basics, best place to know in details is the codeigniter documentation. There you will find in details about every thing with examples also. I found it as one of the best documentation on web. You can ask any kind of question related to codeigniter on their official forum.