Model-View-Controller (MVC)
Relevant source files
The following files were used as context for generating this wiki page:
CodeIgniter 4 is built upon the Model-View-Controller (MVC) architectural pattern. This approach separates the application logic from the presentation, which promotes organized, reusable, and maintainable code. In MVC, the data handling (Model), the user interface (View), and the application control flow (Controller) are managed as three distinct, interconnected components.
The primary goal of this separation is to allow different developers to work on different parts of the application simultaneously without interference. For example, a designer can work on the View files while a developer works on the Controller and Model logic. This structure also makes the application more scalable and easier to debug.
Sources: source/concepts/mvc.rst:3-13
MVC Data Flow
The typical flow of a request in CodeIgniter's MVC architecture involves the user's request first being handled by the routing engine, which then directs it to the appropriate Controller method. The Controller processes the request, interacts with the Model if data is needed, and then loads a View to render the final output sent back to the user.
Sources: source/concepts/mvc.rst:15-19, source/general/routing.rst:10-14
The diagram below illustrates the high-level data flow within the CodeIgniter 4 MVC pattern.
This diagram shows the complete lifecycle of a user request, from initiation to the final response.
Sources: source/concepts/mvc.rst:15-19, source/general/routing.rst:10-14
Components
The MVC pattern is composed of three main components, each with a specific responsibility and a designated location within the application's directory structure.
| Component | Responsibility | Default Directory |
|---|---|---|
| Model | Manages application data and business logic. | app/Models/ |
| View | Displays information to the user. | app/Views/ |
| Controller | Acts as an intermediary, handling user requests. | app/Controllers/ |
Sources: source/concepts/app_structure.rst:20-26, source/concepts/mvc.rst:8-13
Controllers
Controllers are the core of the application's logic, acting as the "traffic cop" between Models, Views, and other resources. When a URL request is received, the routing mechanism determines which controller and method to execute. The controller is responsible for processing input, interacting with the Model to fetch or update data, and then loading the appropriate View to present the results.
A basic controller is a class file that extends CodeIgniter\Controller\BaseController.
<?php
namespace App\Controllers;
class Helloworld extends \CodeIgniter\Controller
{
public function index()
{
return 'Hello World!';
}
}
This example shows a simple controller that returns a string. In a real application, a method would typically load a view.
Sources: source/controllers/controllers.rst:10-29
The sequence of a controller handling a request is as follows:
- The Router maps the URI to a specific public method in a controller.
- The controller method is called, with any URI segments passed as arguments.
- The controller may load models, libraries, or helpers to process the request.
- The controller gathers data and passes it to a View for rendering.
- The rendered View content is sent back to the browser as the final response.
Sources: source/controllers/controllers.rst:10-14, source/general/routing.rst:10-14
Views
A View is a web page, or a fragment of a page like a header, footer, or sidebar. Its primary purpose is to display the information provided by the controller. Views contain minimal PHP code, primarily for looping through data and displaying it. They intentionally separate presentation from application logic.
Views are loaded from within a controller method. Data can be passed from the controller to the view as an array.
// In a Controller method
$data = [
'title' => "My Title",
'heading' => "My Heading",
];
echo view('some_view', $data);
The view() function loads the specified view file and makes the elements of the $data array available as variables (e.g., $title, $heading) within the view file.
Sources: source/views/views.rst:3-10, source/views/views.rst:43-51
Models
Models are responsible for maintaining the application's data and business rules. In CodeIgniter, the Model class provides a set of tools that simplify database interactions. By extending CodeIgniter\Model, your models inherit methods for performing common CRUD (Create, Read, Update, Delete) operations.
A key function of the model is to abstract the database logic away from the controller. The controller should not need to know the specifics of the database schema; it simply asks the model for data.
<?php
namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $allowedFields = ['name', 'email'];
}
This example defines a UserModel that is connected to the users table. The controller can now use this model to interact with user data without writing raw SQL queries.
Sources: source/models/model.rst:3-11, source/models/model.rst:32-49
The interaction between Controller and Model is shown in the sequence diagram below.
This diagram illustrates the controller requesting a user record from the UserModel, which then queries the database and returns the result.
Sources: source/models/model.rst:10-11, source/controllers/controllers.rst:5-8
Summary
The Model-View-Controller pattern is fundamental to building applications with CodeIgniter 4. It provides a clear separation of concerns that leads to cleaner, more organized, and more maintainable code. Controllers handle the request flow, Models manage the data and business logic, and Views are responsible for the final presentation. This structured approach is a key reason for CodeIgniter's reputation as a light, fast, and flexible framework.
Sources: source/concepts/mvc.rst:3-13, README.md:3-4