MVC Concept with Basics of CodeIgniter

Although you have heard this term MVC mentioned in the previous article. MVC is an approach to separating your applications into three segments: Models, Views, and Controllers.

The Model represents any type of data that your application may use. Some examples of data that your application might use would be: a database, RSS Feeds, API calls, and any other action that involves retrieving, returning, updating, and removing data.

Views are the information that is being presented on the screen to users through their web browsers. These are usually HTML files, sometimes containing PHP code that builds the template for the website. In CodeIgniter however, views can be page segments, partial templates, or any other type of page or template.

Finally, Controllers are the business logic of your application. They serve as an intermediary between Models and Views. The Controller will respond to HTTP requests and generate web pages.

In CodeIgniter, Models are not mandatory for database operation. You can do the same inside controller. For small snippets of database operation, you can ignore models for unnecessary complexity. But it is best to practice for you to use models. If your are using a model function multiple times through various controller and you need to change the function, you only need to edit it in one place rather than in all of the controllers.

MVC

Diagram of MVC

Creating a Controller:

Controllers are the core of your application because they determine how HTTP requests should be handled. Let’s dive right in and create a simple Hello World controller. Create a file called Helloworld.php in system/application/controllers/.

<?php
  class Helloworld extends Controller{
     
      function index(){
         echo("Hello, World!");
      }
      
      function hellofunction(){
         echo("Hello This is function under Hellowworld Class");
      }
    
      function display($name){
           echo 'Hi'. $name ;
      }
  }
?>

Explanation of above Code:

Firstly, you can see that the controller is a class that extends the base Controller class. The next thing to note is that there is a function with the name index. This is the default function and will be called when another function has not been called. To see this being run, simply navigate to the URL http://yourwebsite.ext/index.php/helloworld/ and you should see the words Hello, World! on the screen. After that you will see another function named hellofunction()  is method under Helloworld class, you can call this method followed by / (slash) after controller name (http://yourwebsite.ext/index.php/helloworld/hellofunction) The browser will display “Hello This is function under Helloworld Class”.

And finally a method “display()” called with argument $name. Argument pass through URI segment followed by slash after method  i.e (http://yourwebsite.ext/index.php/helloworld/display/bivas) here bivas is the argument and after calling this method the browser will display “Hi Bivas”.

Notes:

All Controller class names should start with an uppercase letter, and the rest of the name should be lowercase. Controllers should always extend the base class so that you can use all of CodeIgniter’s syntax and have access to all CI resources. Not only the class name the controller file name should start with the uppercase letter.

Views:

Views are the simple or complex HTML templates, which can be called by the controller. The controller can call multiple views like header, footer, sidebar etc. for the web page. The view cannot be called directly. Let us create a simple view. Create a new file under application/views with named “example.php” and copy the below given code in that file.

<!DOCTYPE html> 
<html lang = "en"> 

   <head> 
      <meta charset = "utf-8"> 
      <title>My CodeIgnier View</title> 

      <!-- Your Style and Js Script goes through here -->
   </head>
 
   <body> 
      CodeIgniter View Example 
   </body>
 
</html>

You can call the above view from the controller, the syntax of loading a view is:

 $this->load->view("example"); //This is for default location
                               // in view directory

$this->load->view("you_directory_name/example") // For specified directory 
                                                //under view directory

 Let’s see how to call the view from the controller:

 <?php
  class Helloworld extends Controller{
     
      function index(){
         $this->load->view("example");
      }
      
      
  }
?>

Models:

Now that you know what a Model is and what it’s used for, we can take a look at how to construct a Model. Models are stored in system/application/models/. The class of your model should have the first letter capitalised and the rest of the name lowercase, and also extend the base class of Model. The class should also have a constructor that calls upon the base class constructor, as seen in the next code example.

Creating Model Class

 <?php
     class Mymodel extends Model{
        
        function user(){
            $data=array('name'=> "bivas",
                        'Age'=>21,
                        'email'=>"abc@abc.com"
                        );
            return $data;
        }
     }
?>

 Loading a Model

A model is loaded from Controller functions. Loading a model takes just one line of code.

 $this->load->model('model_name');

 In this instance, you should replace model_name with the name of your model. If your model is located in a sub-folder, you would load the model as follows:

 $this->load->model('sub_folder/model_name');

Once loaded, you access your model functions by using the global object with the same name as your model name.

 $this->model_name->function_name();

You can assign a new name for your Model object by passing it to the second parameter of the loading function.

 $this->load->model('model_name', 'different_name');

Now you would call your model functions as follows:

 $this->different_name->function_name();

Here’s an example of a controller calling a model and serving a view.

 <?php
 class Helloworld extends Controller{
    function index(){
         $this->load->model('mymodel');
         $user = $this->mymodel->user();
         $this->load->view('example', $user);
    }
 }
?>

Now modify your example view file to show user data:

 <!DOCTYPE html> 
<html lang = "en"> 

   <head> 
      <meta charset = "utf-8"> 
      <title>View for user</title> 

      <!-- Your Style and Js Script goes through here -->
   </head>
 
   <body> 
      CodeIgniter user View Example 
      <p> User name: <?=$user?> </p>
      <p> User age: <?=$age?> </p>
      <p> User email: <?=$email?> </p>
   </body>
 
</html>

 

Thanks for reading this tutorial. Hope this is helpful for you. If you have any query, please leave a reply.

<<Previous                                                                                                      Next>>

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.