codeigniter
  1. codeigniter-mvc

MVC in CodeIgniter

Model-View-Controller (MVC) is a design pattern used in web applications for separating the application logic from the user interface. CodeIgniter, a popular PHP web framework, follows the MVC pattern to enable developers to create scalable and maintainable web applications.

Syntax

The MVC pattern in CodeIgniter consists of three fundamental components:

Model

The model defines the business logic and interacts with the database to fetch data. It is responsible for reading, writing, updating, and deleting data from the database.

class UserModel extends CI_Model {
  public function getUserById($id) {
    // Fetch user data from the database
  }
}

View

The view is responsible for presenting data to the users in a visually appealing format. It is responsible for displaying data in HTML format and providing user interface components such as forms and buttons.

class UserView extends CI_View {
  public function showUserProfile($user) {
    // Display user data in HTML format
  }
}

Controller

The controller is an intermediary between the view and the model. It receives requests from the user interface and interacts with the model to fetch data. Once data is retrieved, it renders a view to display the results.

class UserController extends CI_Controller {
  public function __construct() {
    parent::__construct();
    $this->load->model('UserModel');
    $this->load->view('UserView');
  }

  public function getUserProfile($id) {
    $user = $this->UserModel->getUserById($id);
    $this->UserView->showUserProfile($user);
  }
}

Example

Consider an example where a website displays the profile of a user when the user visits their dashboard page. To implement this functionality using MVC in CodeIgniter, we would define a UserModel, a UserView, and a UserController.

class UserModel extends CI_Model {
  public function getUserById($id) {
    $this->db->where('id', $id);
    $query = $this->db->get('users');
    return $query->row();
  }
}

class UserView extends CI_View {
  public function showUserProfile($user) {
    ?>
    <h1>User Profile</h1>
    <p>Name: <?php echo $user->name ?></p>
    <p>Email: <?php echo $user->email ?></p>
    <p>Age: <?php echo $user->age ?></p>
    <?php
  }
}

class UserController extends CI_Controller {
  public function __construct() {
    parent::__construct();
    $this->load->model('UserModel');
    $this->load->view('UserView');
  }

  public function userProfile() {
    $id = $this->session->userdata('userId');
    $user = $this->UserModel->getUserById($id);
    $this->UserView->showUserProfile($user);
  }
}

In the example above, when the user visits the userProfile() method, the UserModel is called to retrieve the user data. Once the data is retrieved, the UserView is called to display the data.

Explanation

MVC separates the application logic into three components: the model, view, and controller. This separation enables developers to create scalable and maintainable web applications.

The model is responsible for interacting with the database and fetching data. The view is responsible for presenting the data to the user in a visually appealing format. The controller is an intermediary that receives user requests, interacts with the model to fetch data, and renders the view to display the results.

Use

MVC in CodeIgniter is essential for creating scalable and maintainable web applications. By separating the application logic into three components, it becomes easier to debug, maintain, and extend the code.

Important Points

  • The MVC pattern is essential for creating scalable and maintainable web applications.
  • The model is responsible for interacting with the database and fetching data.
  • The view is responsible for presenting data to the users in a visually appealing format.
  • The controller is an intermediary between the view and the model.

Summary

MVC in CodeIgniter is a fundamental design pattern that separates the application logic into three components: model, view, and controller. The model interacts with the database, the view presents data to the users, and the controller is an intermediary between the two. By using MVC, developers can create scalable and maintainable web applications.

Published on: