1. What the exam task means
The task usually means: create or open a CodeIgniter 4 project, connect it to a MySQL database, fetch records from a table, and display them in a page.
Main flow:
Browser → Route → Controller → Model → Database → View → Web page
Most important concepts
Route
Defines which URL calls which controller method.
Defines which URL calls which controller method.
Controller
Controls the logic and sends data to the view.
Controls the logic and sends data to the view.
Model
Represents a database table.
Represents a database table.
View
HTML page that displays the data.
HTML page that displays the data.
2. CodeIgniter 4 file structure you need
app/
├── Config/
│ └── Routes.php
├── Controllers/
│ └── Students.php
├── Models/
│ └── StudentModel.php
└── Views/
└── students.php
.env
database.sql
Important: CodeIgniter 4 is not the same as CodeIgniter 3.
Avoid exam examples using
CI_Controller, CI_Model, or $this->load.
3. Database SQL example
Create a database and table. Example table: students.
CREATE DATABASE school;
USE school;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL
);
INSERT INTO students (name, email)
VALUES
('Ana', 'ana@test.com'),
('Marko', 'marko@test.com'),
('Sara', 'sara@test.com');
4. Database connection in .env
In CodeIgniter 4, database settings are usually placed in the .env file.
CI_ENVIRONMENT = development
database.default.hostname = localhost
database.default.database = school
database.default.username = root
database.default.password =
database.default.DBDriver = MySQLi
database.default.port = 3306
If the database does not connect, check database name, username, password, and whether MySQL is running.
5. Route pattern
File: app/Config/Routes.php
$routes->get('/students', 'Students::index');
| Part | Meaning |
|---|---|
/students |
The URL path in the browser. |
Students |
The controller class. |
index |
The method/function inside the controller. |
6. Model pattern
File: app/Models/StudentModel.php
<?php
namespace App\Models;
use CodeIgniter\Model;
class StudentModel extends Model
{
protected $table = 'students';
protected $primaryKey = 'id';
protected $allowedFields = ['name', 'email'];
}
Important model methods
| Method | Use |
|---|---|
findAll() |
Gets all rows. |
find($id) |
Gets one row by ID. |
insert($data) |
Adds a new row. |
update($id, $data) |
Updates a row. |
delete($id) |
Deletes a row. |
7. Controller pattern
File: app/Controllers/Students.php
<?php
namespace App\Controllers;
use App\Models\StudentModel;
class Students extends BaseController
{
public function index()
{
$model = new StudentModel();
$data = [
'students' => $model->findAll()
];
return view('students', $data);
}
}
What this does
use App\Models\StudentModel;imports the model.$model = new StudentModel();creates the model object.$model->findAll()gets all students from the database.return view('students', $data);opens the view and sends data to it.
8. View pattern
File: app/Views/students.php
<!DOCTYPE html>
<html>
<head>
<title>Students</title>
</head>
<body>
<h1>Students</h1>
<?php foreach ($students as $student): ?>
<p>
<?= esc($student['name']) ?>
-
<?= esc($student['email']) ?>
</p>
<?php endforeach; ?>
</body>
</html>
Use
esc() when printing user/database data. It helps prevent unsafe HTML output.
9. Full minimal pattern to memorize
Route
$routes->get('/students', 'Students::index');
Model
class StudentModel extends Model
{
protected $table = 'students';
protected $primaryKey = 'id';
protected $allowedFields = ['name', 'email'];
}
Controller
$model = new StudentModel();
return view('students', [
'students' => $model->findAll()
]);
View
<?php foreach ($students as $student): ?>
<p><?= esc($student['name']) ?></p>
<?php endforeach; ?>
10. Extra CRUD examples
Insert data
$model->insert([
'name' => 'Luka',
'email' => 'luka@test.com'
]);
Find one row
$student = $model->find(1);
Update row
$model->update(1, [
'name' => 'Updated Name'
]);
Delete row
$model->delete(1);
11. PHP operators you will see
| Operator | Meaning | Example |
|---|---|---|
-> |
Access object method/property | $model->findAll() |
=> |
Array key/value | 'students' => $students |
:: |
Static/class method reference | Students::index |
12. Common errors and fixes
| Error | Likely cause | Fix |
|---|---|---|
| 404 page not found | Wrong route or controller name | Check Routes.php and controller class name. |
| Class not found | Wrong namespace or filename | Check namespace App\Models; and use App\Models\StudentModel;. |
| Database connection failed | Wrong .env details |
Check database name, username, password, and MySQL server. |
| Undefined variable: students | Controller did not send data to view | Use return view('students', ['students' => ...]);. |
| No data displayed | Empty table or wrong table name | Check SQL data and protected $table. |
13. Exam checklist
☐ MySQL is running
☐ Database exists
☐ Table has data
☐
.env database settings are correct☐ Route points to controller
☐ Controller imports model
☐ Model has correct table name
☐ View uses correct variable name
14. Good Google searches during exam
CodeIgniter 4 model findAll example
CodeIgniter 4 database .env MySQLi
CodeIgniter 4 routes controller method
CodeIgniter 4 pass data to view
CodeIgniter 4 foreach view example
CodeIgniter 4 esc output
Avoid broad searches like
CodeIgniter database tutorial, because many results may be CodeIgniter 3.