How does MVC work in Codeignitor?

PHP

Controller:As the name suggests, it controls the relationship between views and models. The entire URL works in CodeIgniter with the controller name. Now let's take a look at some of the things we need to consider when creating a new controller.
  • When we create a class in a controller, the class name must be the same as the file name, and it must start with a capital letter, otherwise we will get an error while executing our project.
  • After the class is declared, we inherit all the properties of the CI_Controller with the extends keywords to use all the properties of the CI_Controller (see PHP | Common Terminology in OOP ). There will be no problem if we do not extend CI_Controller, but in this case we will not be able to access any property of CI_Controller.
  • Each controller has a default function index that starts automatically when we only define the name of the controller in the URL. For the rest of the functionality, we have to mention this manually after the controller name in the URL.
  • Model:The model is responsible for handling backend related requests. All requests or backend functions like databases, APIs that we write in models. Now let's look at some of the things we need to consider when creating a new model.
  • When we create a class in a model, the class name must be the same as the file name, and it must start with a capital letter, otherwise we get an error while executing our project.
  • Once the class is declared, we inherit all the CI_Model properties with the extends keywords to use all the CI_Model properties.
  • When we create a function in the model, make sure that it must be public, otherwise it will not be available in the controller.
  • To access the model in the controller, it must first be loaded, after loading the model, we can call the function of that particular model in the controller. Below is the line to load the model into the controller.
    $this-> load-> model ('post_model'); 
    Below is the line to load the model in autoload.php, which is available in the folder config. If we need a global model in our project, use autoload.php.
    $autoload ['model'] = array (' post_model'); 
  • Presentation:The presentation is part of the presentation in the MVC concept. Whatever is client-side like HTML, CSS or JavaScript used in the file will be displayed to the user through the view part. The file preview is saved in the application / view folder. When the model fetches data from the backend and returns it to the controller, the controller passes all the data in the view page.With that said, we'll see an example of how MVC works in CI.Example: Controller file:Create a users.phpfile in the Controller folder and write the code below. 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 to getUsers method from usermodel $data [ 'users' ] = $this -> usermodel-> getUsers(); $this -> load -> view ( 'users_list' , $data ); }  
    }  
    ?>
    Model file:create a file usermodel.phpin the Model folder and write down the following code. 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' ], ]; }  
    } 
    ?>
    View file:create file users_list.phpin the View folder and write down the following code. "en" > < title > User List < / title > < / head > < body > " border: 1px solid; " > foreach ( $users as $row ) {?>
    " border: 1px solid; " > echo $row [ 'firstname' ]; ?> < / td > " border: 1px solid; " > echo $row [ 'lastname' ]; ?> < / td >
    < / body >
    < / html >
    Now run http: // localhost / ci_tutorial /index.php/Usersin the browser. 
    Exit: