×

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user via HTTP and passes these requests to the model and view to provide a relevant result to the user. Due to this, it is the main part of the CodeIgniter application, which handles the server request. A controller also contains an action or function to perform a particular task. And you can define multiple methods in the controller class. Furthermore, a controller can also load the model and the view file.  

Create a Controller

Here we have described some steps for creating a Controller.

You can use visual studio, notepad++, sublime, or any other editors for creating CodeIgniter web application. Here we are using visual studio for developing a web application.

Firstly, go to the CodeIgniter-3.1.11 folder and click on it. Then you will see, three folder application, system, and user_guide

Controller

After that, click on the application folder, where you will see many sub-folders inside the application folder like the given below image.

Controller2

Now click on the Controller folder, and then, you will see some predefined files, as shown in the given below image.

Controller3

Now, Right-click on the controller’s folder and create a New File, as shown in the given below image.

Controller4

Now you can define a controller file name with the php extension. In this, we have created an Users.php file of the controller. You should remember that the first letter of the file in uppercase, as we have taken in the image given below.

Controller5

After providing the name of the controller file, press the Enter button from your keyboard. And then, your screen will look like the image given below.

Controller6

Example: Create a Users.php file inside the Application/Controller and then write the given below code in your file.

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Users extends CI_Controller {    // here Users is the controller name which extends the //CI_Controller
             public function index()  // index is the action name  
           { 
             echo "This is my first Controller"; 
             }
 }
 ?> 
defined('BASEPATH')
OR exit('No direct script access allowed');

The above statement shows that a controller can’t be accessed directly. It means if you want to access or run your controller in the web browser, then you have to define the index.php file before the controller’s name. An index.php file is the main file through which the queries pass to the controller. Therefore, you have to always use an index.php file in the localhost's URL before calling the controller.

Now run your code on the localhost server by writing this localhost/CodeIgniter-3.1.11/index.php/Users/index

Output

Controller7

In the above output, CodeIgniter-3.1.11 is the primary folder name in which all your sub-folder present, index.php file is the main file through which your queries pass to the controller, Users is the Controller name, and an index is the method name. 

Note: You can also define the multiple actions inside the controller class.

Example: Create multiple methods in the Users’ Controller class.

<?php
 class Users extends CI_Controller {
             public function index()
             {  
         echo "This is my first Controller"; 
     }
     public function second()
             { 
         echo "This is my Second Controller"; 
     }
     public function last()
             { 
         echo "Welcome to the Tutorials and Example"; 
             }
 } 
 ?> 

In the above program, we have created multiple methods like index, second, and last. Now we will execute all the methods one by one.

localhost/CodeIgniter-3.1.11/index.php/Users/second

Output

Controller8

localhost/CodeIgniter-3.1.11/index.php/Users/last

Output

Controller9

Related Topics

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.