CodeIgniter Laravel PHP Example HTML Javascript jQuery MORE
Need a Website Or Web Application.Contact : +91 9437911966 (Whatsapp) Note: Paid Service

Controller in CodeIgniter framework


CodeIgniter framework is based on MVC(Model-View-Controller) pattern. So first of all we are going to discuss about controller.

It is the intermediary between models and view to process HTTP request and show a web page. All the requests received by the controller are passed on to models and view to process the information. In other words we can tell it is the heart of the application.

It is a simple class file. The name of the class is associated with URI.

Let’s see the below example to better know about controller.

Example:

Hello.php

$('#myTable').DataTable( {
	buttons: [
		{ extend: 'copy', text: 'Copy to clipboard' },
{ extend: 'print', text: 'Print Now' },
	]
} );

Here my controller file name is Hello.php and my class name is Hello. So in to run the file my URL should be Like:

example.com/index.php/Hello/

Output is :

Hello World!

Note: Class names must start with an uppercase letter. Valid Controller:

class Hello extends CI_Controller {

}
In valid Controller:
class hello extends CI_Controller {

}

Method:

In the above Hello World example index() is the method. If you run with only the class name in URL by default the index() method will be execute. If you want to add another method then we have to create another function like the below example.

<?php
class Hello extends CI_Controller {

        public function index()
        {
                echo 'Hello World!';
        }
        public function about()
        {
                echo 'Tutorial Site!';
        }

}


Then to get the output of the second method the URL be like:

example.com/index.php/Hello/about/

Passing URI Segments to methods

If your URI contains more than two segments they will be passed to your method as parameters.

For example, let’s say you have a URI like this:

example.com/index.php/products/shoes/sandals/123

Your method will be passed URI segments 3 and 4 (“sandals” and “123”):

<?php
class Products extends CI_Controller {

        public function shoes($sandals, $id)
        {
                echo $sandals;
                echo $id;
        }
}

Defining a Default Controller

CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes.php file and set this variable:

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

Where ‘about’ is the name of the controller class you want used. If you now load your main index.php file without specifying any URI segments you’ll see your “Tutorial Site!” message by default.

Remapping Method Calls

As noted above, the second segment of the URI typically determines which method in the controller gets called. CodeIgniter permits you to override this behavior through the use of the _remap() method:

public function _remap()
{
        // Some code here...
}

If your controller contains a method named _remap(), it will always get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which method is called, allowing you to define your own method routing rules.

The overridden method call (typically the second segment of the URI) will be passed as a parameter to the _remap() method:

public function _remap($method)
{
        if ($method === 'some_method')
        {
                $this->$method();
        }
        else
        {
                $this->default_method();
        }
}