Codeigniter - SPLessons

CodeIgniter Controller

Home > Lesson > Chapter 8
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Controller

CodeIgniter Controller

shape Description

CodeIgniter Controller is a basic class file that acts as the glue between the View and the Model. The name itself implies that the entire CodeIgniter application is managed by Controller. It gets the request from the Model, performs various actions and passes the data to the View and renders the actual page.

Creating CodeIgniter Controller

shape Description

Initially, navigate to the folder application/controllers folder, index.html and Welcome.php are the two inbuilt files that comes with the CodeIgniter. Now, create a new file "splessons.php" in the same folder and enter the below code: [php] <?php class Splessons extends CI_Controller { public function index() { echo "Hello! Welcome to SPLessons"; } } ?> [/php] Output: [php]Hello! Welcome to SPLessons[/php] In the above example, controller Splessons is created that extends CI_Controller which is a in-built class of CodeIgniter and has to be extended when own classes are created. The name of the controller should start with the capital letter.

Calling Controller

The "Splessons" controller in the above example will display an output when called by the below URI in the browser : [html]http://localhost/Codeigniter/index.php/splessons[/html] In the above URI, "splessons" is the class name of CodeIgniter Controller or the name of controller. But in URI, the controller should be of lower case. The syntax for calling a controller will be as follows. [html]http://www.your-domain.com/index.php/controller/method-name[/html]

Creating & Calling Constructor Method

shape Description

To learn how to call constructor method, create other method called "hi". [php] <?php class Splessons extends CI_Controller { public function index() { echo "This is default function."; } public function hi() { echo "This is hi function."; } } ?> [/php] To execute the above controller, below mentioned URI's can be entered in the browser and the following outputs are observed. (i) index.php/splessons [html] http://www.your-domain.com/index.php/splessons [/html] When above URI is entered the output will be "This is default function" which is nothing but "index" method output which appears automatically as it is the default method of CodeIgniter. (ii) index.php/splessons/index [html] http://www.your-domain.com/index.php/splessons/index [/html] This URI is similar to the case 1 and the output will be "This is default function" which is nothing but "index" method output that appears after entering the method name manually. (iii) index.php/splessons/index/splessons/hi [html] http://www.your-domain.com/index.php/splessons/hi [/html] When above URI is entered the output will be "This is hi function" which is nothing but "hi" method output that is present in the Splessons controller as the user's own method.

Initialization method

shape Description

When there is a requirement to apply single condition to each and every method present in the controller, a method called __construct() is used. [php] <?php class Splessons extends CI_Controller { public function __construct(){ parent::__construct(); echo "This is initialization method<br>"; } public function index() { echo "This is default function."; } public function hi() { echo "This is hi function."; } } ?> [/php] The URI's can be any of the three mentioned below. [html] http://www.your-domain.com/index.php/splessons http://www.your-domain.com/index.php/splessons/index http://www.your-domain.com/index.php/splessons/hi [/html]

Summary

shape Key Points

  • CodeIgniter Controller class name begins with a uppercase letter.
  • The controller is called in URI with a lowercase letter.
  • The method __construct() can be used to apply the same functionality when calling every method.