Part I: CodeIgniter Basics

Submitted on Fri, 08/14/2020 - 16:44

Filesystem

Inside the CodeIgniter directory ci, there is a directory called application

This is the parent directory where our application will reside.

Inside this directory, there are four subdirectories which we sould be concerned with for now.

1. config

This directory has two files concerned with initial codeigniter configuration.

1.1 config.php

We need to define our base site URL in this file. This is a must. In our case, the file should read:

$config['base_url'] = 'http://localhost/ci/'; 

Note: Base URL should end with a trailing slash

1.2 database.php

We will define our database connection variables in this file.

2. controllers

This directory will contain all the controller files.

3. models

This directory will contain all the model files.

4. views

This directory will contain all the view files.


Naming Convention and class inheritance:

  • Controller and model classnames should start with uppercase letter.

  • Controller and model filenames should be same as the classnames.

  • However while refering to php files, lowercase letters are used without php extension.

    An example:
    roller refers to Roller.php controller file
    connect refers to Connect.php model file
    home refers to home.php view file (view filename can start with lowercase letter.)

  • CodeIgniter has readymade classes CI_Controller and CI_Model for controllers and models.

  • Controller class should extend CI_Controller. Likewise, model class should extend CI_Model.

 

An example:

The directory application/contollers has a file called Roller.php.

The file has following class defined:

class Roller extends CI_Controller {}

Likewise,

The directory application/models has a file called Connect.php.

The file has following class defined:

class Connect extends CI_Model {}

This verifies that Roller is a controller class and Connect is a model class.


URL Resolution

Let us look at how codeigniter resolves a URL.

<base_URL>/index.php/<controller>/<function>/<arugment>

If the function has two arguments it would be passed as follows:

<base_URL>/index.php/<controller>/<function>/<arugment1>/<arugment2>

 

An example:

http://localhost/ci/index.php/roller/show/1

Based on the URL structure we can derive following parameters,

controller: roller

function: show()

argument: 1

CodeIgniter will look for a file Roller.php in the directory application/controllers.

In the file there should be a class named Roller.  The class should extend the controller class CI_Controller.

It shall contain a function show()

The value passed as an argument to this function is 1.

Hence, the file may look something like this:

<?php

class Roller extends CI_Controller 

{

public function show($id)

{

echo " The value passed is: ".$id;

}

}

?>

URL: 

http://localhost/ci/index.php/roller/show/1

Output: 

The value passed is: 1

Thus we can say that only controller can be accessed through URL. Views and models are accessed by calling functions of controller which load them.

Note: If the function called through URL takes argument we must pass it through the URL. In above example, if URL was http://localhost/ci/index.php/roller/show we would receive error about missing argument.


URL Rewriting

We can omit index.php from the URL by using URL rewriting feature of the web server.

If we assume that we are using apache2 as our web server, we can create a file named .htaccess in the parent directory ci. The file shall contain following content:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

 

Explanation:

RewriteEngine On

This directive makes sure that the rewrite engine is on.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

These two directives define conditions when reqested resource is neither a file nor a directory.

RewriteRule ^(.*)$ index.php/$1 [L]

^(.*)$ refers to the requested resource.

$1 is back reference to the requested resource ^(.*)$

[L] marks the rewriting rule to be last one so that no further URL rewriting occurs.

An example:

http://localhost/ci/logo.jpg

Here ^(.*)$ refers to logo.jpg

In this case the requested resource, logo.jpg is a file.

Thus the rewriting conditions are not met and URL rewritting is skipped.

 

Another example:

http://localhost/ci/roller/show/1

Here ^(.*)$ refers to roller/show/1

The requested resource is neither a file nor a directory. 

Here, the rewriting conditions are met. Hence as per rewrite rule, the URL is rewritten to from ^(.*)$ to index.php/$1 or from roller/show/1 to index.php/roller/show/1

Hence, with URL rewriting enabled, http://localhost/ci/roller/show/1 is rewritten to http://localhost/ci/index.php/roller/show/1


Default function

In CodeIgniter index() is the default function for any controller. That means, index function doesn't need to be called through URL. Or, when no function is called through URL, index function is called.

For example,

 

<?php

class Roller extends CI_Controller 

{

public function index()

{

echo "This function is called by default.";

}

}

?>

In this case to access the index function we simply need to type the following URL:

http://localhost/ci/roller

Output:

This function is called by default.

Note: If the URL was http://localhost/ci/roller/index, the output would still be same.

 

If the index function takes argument, we have to specify it with the index function in the URL.

Example:

<?php
class Roller extends CI_Controller 

    {

    public function index($id)

        {

        echo 'The argument passed to index function is: '.$id;

        }
    
    }

?>

URL:

http://localhost/ci/index/432

Output: 

The argument passed to index function is: 432

 

Now let us take another example:

<?php
class Roller extends CI_Controller 

{

public function show()

{

echo "This function has to be called explicitly";

}

}

?>

In order to access the function show() of the controller class Roller, we will need to type following URL:

http://localhost/roller/show/

Output:

This function has to be called explicitly

Note: In the above example, if the accessed URL was http://localhost/roller/ we would receive 404 error since there is no index() function which would be called by default.


Constructor function

Controllers and models can also have constructor function in them. Constructor functions are called before any other function in the class. 

The main difference between constructor function and index function is that constructor function is called first whereas index function is called by default. Likewise, consructor function cannot be called through URL. It is not compulsory to have this function however, it can be useful to load default resources, before any other function is called from the class. Resources defined in the constructor function are available to all the other functions of the given class.

An example:

<?php

class Roller extends CI_Controller 

{

public function index()

{

echo "This function will be called by default.";

}

public function __construct()

{

echo "This function will be called first before any other function.<br/>";

}

}

?>

URL: http://localhost/ci/roller

Output:

This function will be called first before any other function.

This function will be called by default.


Default controller

In case we dont provide controller through URL, CodeIgniter will use the default controller.

The default controller is defined in the file routes.php located in the directory application/config/

If we want Roller.php to be the default controller file we need to have the following line in routes.php

$route['default_controller'] = 'roller';

Note: roller refers to Roller.php controller class

Thus, when we type http://localhost/ci, we are actually calling the default function of the default controller, roller in this case.

Note: This is how the first welcome message of CodeIgniter is loaded in the browser.


Controller and view

Now let us observe how controller works with view files.

Let us create a view file named greetings.php which simply outputs some text to the user as follows:

<html>
<head><title>A view file</title></head>
<body>
<h1>Welcome to CodeIgniter</h1>
</body>
</html>

Output: Welcome to CodeIgniter

Note: Even though the language used in the above file is html only, the file needs to be saved as php file since CodeIgniter is a php framework.

Now what we need to understand is that we cannot access this view file from the web browser on its own.
For that we need to build a controller and function which loads this view.

Let us create a controller file called Hello.php

We can start by defining class with same name as the controller filename which should extend the readymade CI controller class, CI_Controller

<?php
class Hello extends CI_Controller
{
}
?>

Now we will define the default index function which will load the view greetings.php.

<?php
class Hello extends CI_Controller
{
    public function index()
    {
    $this->load->view('greetings');
    }
}
?>

Note: Here, $this->load refers to loader object which has specific functions to load view and model. Here we used the function view() to load the view file,greetings.php.

Now when we access http://localhost/ci/hello/, default function index() is called and the view is loaded in the browser.


Part II: Application development

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.