What is MVC in CodeIgniter Framework

MVC  stands for Model View Controller, and It is a programming pattern in which we divide our project into three folders. It is best suitable for a medium or large project in which more than one developer will work.

  • Model: In the Model folder, we contain our database-related files, such as the crud file in which we write insert, update, delete queries.
  • View: In the View folder, we contain our designing files such as index.php, about.php, contact.php, etc.
  • Controller: In this folder, we contain our logical files in which we write our logic such as file upload, contact form mail, etc. It works as an intermediate between views and models.

 

The following are some advantages of using MVC Pattern:

1.  It is best suitable for large projects: on which multiple developers can work efficiently means designer work in views folder, a PHP developer can work in the controller folder. The database developer can quickly work in the model's folder without any interruption.

2. Flexibility:  It is flexible means we can increase flexibility because it categorizes our project. Our project is well organized in MVC. Our design is in the views folder, and the controller has logic; the model has database-related code so anybody can easily change their components.

 

Let's see the Details of Each with an example:

1. Model:  As we have already said Model is used to work with database-related code such insert data in the database, editing data in the database, delete data in the database, select data from the database.

In CodeIgniter, we can use the model file in two ways:

  • By loading model in controller file function
Example:  $this->load->model(“crud”);
  • By autoloading in autoload.php file
Example: $autoload['model'] = array('crud');

Most developers recommend using model files by autoloading in autoload.php because it saves time and code repetition.

Example: CRUD  file of the model in CodeIgniter

<?php
class Crud extends CI_Model{
function insert($table,$data)
            {
                        $result= $this->db->insert($table, $data);
                        return $result;
            }
           
            function update($table,$id,$data)
            {
                        $this->db->where('id', $id);
                        return $this->db->update($table,$data);
            }
 
 
 
            function get_data($table)
            {  
               $data= $this->db->get($table);
               return $data->result();               
            }
 
           
            function delete($table,$id)
            {
                        $this->db->where('id', $id);
                        return $this->db->delete($table);
            }
           
 
           
}
 

 

2. Views: View is responsible for containing your designing file, and it is run by using controllers functions. In MVC, we can not run our file directly, and we have to use the controller function to run our file.

Example: index.php file in the views folder

<!DOCTYPE html>
<html>
<head>
<title>CodeIgniter first view</title>
</head>
 
 
<body>
<h1>CodeIgniter first file</h1>
</body>
</html>
 
Controllers Code look like this to run this view:
public function index()  
{
  $this->load->view('index');
}
 

3.Controller: The controller act as an intermediate between model and view. It contains logic to run view and to connect with model file functions.

Example: Welcome.php Controller of CodeIgniter to run index.php view file

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Welcome extends CI_Controller {
public function index()  
{
  $this->load->view('index');
}
 
}
 

Important Note: Developer should learn OOP for better understanding of MVC, because in MVC pattern we use classes, extends, construct, etc. These are OOP programming concepts, so we recommend all beginner developers should learn OOP concepts.

Categories: web development codeigniter

Trending Courses

CodeIgniter

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Advance Digital Marketing

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 20 Days

Crash : 7 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 7 Month

Fastrack : 4 Month

Crash : 2 Month

Related Blogs

Request For Demo