Create Database
Create new database named codeigniter4_db. This database have 1 table: Product table.
--
-- Table structure for table `product`
--
CREATE TABLE `product` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
`price` double NOT NULL,
`quantity` int(11) NOT NULL,
`status` tinyint(1) NOT NULL,
`created` date NOT NULL,
`description` text NOT NULL,
`category_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `product`
--
INSERT INTO `product` (`name`, `price`, `quantity`, `status`, `created`, `description`, `category_id`) VALUES
('Tivi 1', 20, 4, 1, '2021-07-13', 'good', 1),
('Tivi 2', 63, 15, 0, '2020-08-11', 'good', 1),
('Laptop 1', 24, 12, 1, '2021-08-12', 'good', 2),
('Laptop 2', 60, 34, 1, '2020-08-12', 'good', 2),
('Laptop 3', 23, 27, 0, '2020-08-17', 'good', 3),
('Computer 1', 4.5, 2, 1, '2021-08-20', 'ABC', 3),
('Computer 2', 111, 222, 1, '2021-09-25', 'AAA', 7),
('Computer 3', 33333, 888, 0, '2021-10-11', 'abc', 2),
('ABC', 20, 4, 1, '2021-07-12', 'good', 7),
('ABC', 20, 4, 1, '2021-07-12', 'good', 7),
('ABC', 20, 4, 1, '2021-07-12', 'good', 7),
('ABC', 20, 4, 1, '2021-07-12', 'good', 7),
('ABC', 20, 4, 1, '2021-07-12', 'good', 7);
Structure of Product Table
Data of Product Table
Download and Install CodeIgniter 4
Download the latest version of CodeIgniter 4 and unzip source code to new folder named LearnCodeIgniter4WithRealApps
Cut index.php and htaccess files in public folder to root folder of project
Open index.php in root folder find to line 16 replace path to Paths.php file as below:
$pathsPath = realpath(FCPATH . '/app/Config/Paths.php');
Open App.php in app/Config folder find to line 39 remove index.php string in $indexPage variable as below:
public $indexPage = '';
Set BASE URL
Open App.php file in app/Config folder. Set value for $baseURL variable as below:
public $baseURL = 'http://localhost:8091/LearnCodeIgniter4WithRealApps/';
Configure Database Connections
Open Database.php file in app/Config folder and add values as below connect to database:
<?php
public $default = [
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'codeigniter4_db',
'port' => 3306,
];
Create Controller
Create new PHP file named Demo.php in app/Controllers folder as below:
<?php
namespace App\Controllers;
class Demo extends BaseController
{
protected $table = 'product';
private $db;
public function __construct()
{
$this->db = \Config\Database::connect();
}
public function index()
{
$data['product'] = $this->db->table($this->table)->where(["id" => 1])->get()->getRow();
return view('demo/index', $data);
}
}
Create Views
Create new folder named demo in app/Views folder. In this folder, create new PHP file named as below:
Index View
In demo folder, create new PHP file named index.php as below:
<html>
<head>
<title>Query Builder Class in CodeIgniter 4</title>
</head>
<body>
<h3>Product Info</h3>
<table border="1" cellpadding="2" cellspacing="2">
<tr>
<th>Id</th>
<th>Name</th>
<th>Status</th>
<th>Created</th>
<th>Price</th>
<th>Quantity</th>
<th>Description</th>
</tr>
<tr>
<td><?= $product->id ?></td>
<td><?= $product->name ?></td>
<td><?= $product->status ?></td>
<td><?= $product->created ?></td>
<td><?= $product->price ?></td>
<td><?= $product->quantity ?></td>
<td><?= $product->description ?></td>
</tr>
</table>
</body>
</html>
Define Routes
Open Routes.php file in app/Config folder. Set default controller as below:
$routes->get('/', 'Demo::index');
$routes->get('/demo', 'Demo::index');
$routes->get('/demo/index', 'Demo::index');
Structure of CodeIgniter 4 Project
Run Application
Access index action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/index
Output