How MVC works in Codeignitor ?

Codeignitor is based on MVC design pattern. We will see how the controller, model, and view work and display the result to the user.

Controller: Like the name suggest, it controls the communication between views and models. All the URL works in CodeIgniter with controller name. Now let’s see some point which we need to keep in mind when we create a new controller.

  1. When we create a class in a controller the name of the class should match with the file name and it should start with upper case else we will receive an error while running our project.
  2. Once the class is declared we inherit all the properties of CI_Controller with extends keywords to use all the properties of CI_Controller(Go through the PHP | Common terminology in OOP to understand inheritance). There won’t be any issue if we won’t extend CI_Controller but in that case, we won’t be able to access any property of CI_Controller.
  3. Every controller has a default function index which runs automatically when we define only controller name in URL. For the rest of the functions, we need to mention it manually after controller name in URL.

Model: Model is responsible to handle the queries related with backend. All the queries or functionalities of the backend like database, APIs we write in models. Now let’s see some point which we need to keep in mind when we create a new model.



  1. When we create a class in a model the name of the class should match with the file name and it should start with upper case else we will receive an error while running our project.
  2. Once the class is declared we inherit all the properties of CI_Model with extends keywords to use all the properties of CI_Model
  3. When we create a function in model make sure it should be public else it won’t be accessible in controller.
  4. To access the model in a controller it need to be loaded first, after loading the model we can call the function of that specific model in controller. Below is the line to load the model in a controller.
     $this->load->model('post_model');

    Below is the line to load model in autoload.php which is available in config folder . If we need the model globally in our project, then use autoload.php.

    $autoload['model'] = array('post_model');

View: View is presentation part in MVC concept. Whatever is at client end like HTML, CSS or JavaScript is used in the file will be displayed to the user through view part. View files are saved in the application/view folder. When model fetches data from backend and returns it to the controller then controller passes all the data in view page.

Keeping all the above point in mind we will see an example of how MVC works in CI.

Example:

Controller File: Create a file users.php in Controller folder and write down the code below.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
  
class Users extends CI_Controller {
          
    public function __construct(){
       parent::__construct();
           //load model
           $this->load->model('usermodel');
    }
  
    public function index()
    
          //access the getUsers method from usermodel
           $data['users'] = $this->usermodel->getUsers();
           $this->load->view('users_list', $data);
    }
      
}
  
?>

chevron_right


Model File: Create a file usermodel.php in Model folder and write down the code below.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
class Usermodel extends CI_Model {
         
       public function getUsers() 
       {
         // Here we can also run database queries and return the result  
         return [
           ['firstname'=>'First User', 'lastname'=>'First Name'],
           ['firstname'=>'Second User', 'lastname'=>'Second Name'],
           ['firstname'=>'Third User', 'lastname'=>'Third Name'],
         ] ; 
           
       }
         
}
  
?>

chevron_right


View File: Create a file users_list.php in View folder and write down the code below.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html lang="en">
  <head>
    <title>User List</title>
  </head>
<body>    
   <table style="border: 1px solid;">
     <?php foreach ($users as $row) { ?>
      <tr>
         <td style="border: 1px solid;">
            <?php echo $row['firstname']; ?>
         </td>
          <td style="border: 1px solid;">
            <?php echo $row['lastname']; ?>
         </td>
      </tr>
     <?php } ?>
   </table>
</body>
</html>

chevron_right


Now run http://localhost/ci_tutorial/index.php/Users in the browser.
Output:



My Personal Notes arrow_drop_up

Web Developer, Technical Content Engineer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.