Codeigniter 4 Create Controller, Model, View Example
In this tutorial, we will show you how to create a CodeIgniter 4 application with the MVC (Model-View-Controller) pattern. CodeIgniter 4 is a PHP framework that is built using the MVC architecture, which separates the application logic into three distinct parts: the Model, the View, and the Controller.
The Controller acts as the middleman between the Model and the View, passing data back and forth as needed. It handles user requests and processes data, then passes the results to the View for display.
The Model manages the data of the application, enforcing business rules and storing data in the database. It is responsible for retrieving, updating, and deleting data.
Finally, the View is responsible for displaying the data to the user. It is a template file that is rendered by the Controller and displays the data in a user-friendly format.
By separating the application logic into these three distinct parts, CodeIgniter 4 promotes code reusability and maintainability. This tutorial will show you how to create each part of the MVC architecture in CodeIgniter 4, so that you can build robust and scalable web applications.
Views are simple files, with little to no logic, that displays the information to the user.
Create a Controller in CodeIgniter 4
All Controllers are typically saved in /app/Controllers. If you want to create a new controller in codeigniter 4. So go to app/controller and create a new php file and update the following code:
<?php namespace App\Controllers;
use CodeIgniter\Controller;
class Blog extends Controller
{
public function index()
{
echo 'Hello World!';
}
}
Create a Model in CodeIgniter 4
All Models are typically saved in /app/Models. If you want to create a new model in CodeIgniter 4. So go to /app/Models and create a new PHP file and update the following code:
<?php namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
}
CodeIgniter 4 Create a View
All views are typically saved in /app/Views. If you want to create a new views in CodeIgniter 4. So go to /app/Views and create a new PHP file and update the following code:
<html>
<head>
<title>My Blog</title>
</head>
<body>
<h1>Welcome to my Blog!</h1>
</body>
</html>
Conclusion
In this tutorial, you have learned how and where to create controllers, models, and views in CodeIgniter 4 framework.
You May Also Like

