Codeigniter 4 Create Controller, Model, View Tutorial

Apr 25, 2022 . Admin



Hi Guys,

Today, how to create a model view controller in Codeigniter 4 is our main topic. you can see Codeigniter 4 create a controller. this example will help you model. Here you will learn to view examples.

Like most web frameworks, CodeIgniter uses the Model, View, Controller (MVC) pattern to organize the files.

  • Models
  • Models manage the data of the application and help to enforce any special business rules the application might need.

  • Views
  • Views are simple files, with little to no logic, that display the information to the user.

  • Controllers
  • Controllers act as glue code, marshaling data back and forth between the view (or the user that’s seeing it) and the data storage.

Step 1: Install Codeigniter 4

This is optional; however, if you have not created the codeigniter app, then you may go ahead and execute the below command:

composer create-project codeigniter4/appstarter ci-news
Step 2: Create a Controller in CodeIgniter 4

In this second step we need to create a blog Controllers are typically stored in /app/Controllers, though they can use a namespace to be grouped however you need.

/app/Controllers/Blog.php
<?php 
namespace App\Controllers;
use CodeIgniter\Controller;
 
class Blog extends Controller
{

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        echo 'Hello World!';
    }
}
Create a Model in CodeIgniter 4

Models are typically stored in /app/Models, though they can use a namespace to be grouped however you need.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:

/app/Models/UserModel.php
<?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 Our Website!</h1>
</body>
</html>

It will help you.....

#Codeigniter 4