×

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 String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 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.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to...

3 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 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.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

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 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.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute 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 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.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 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.

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.