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/';
Create Controller
Create new PHP file named Demo.php in app/Controllers folder as below:
<?php
namespace App\Controllers;
class Demo extends BaseController
{
public function __construct()
{
helper(['url', 'form']);
}
public function index()
{
return view('demo/index');
}
public function process_login()
{
$username = $this->request->getPost('username');
$password = $this->request->getPost('password');
if ($username == 'pmk' && $password == 'lab') {
session()->set('username', $username);
return $this->response->redirect(site_url('demo/welcome'));
} else {
$data['error'] = 'Invalid Account';
return view('demo/index', $data);
}
}
public function welcome()
{
return view('demo/welcome');
}
public function logout()
{
session()->remove('username');
return $this->response->redirect(site_url('demo/index'));
}
}
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>Login Form in CodeIgniter 4</title>
</head>
<body>
<h3>Login Form</h3>
<?= isset($error) ? $error : '' ?>
<?= form_open('demo/process_login') ?>
<table cellpadding="2" cellspacing="2">
<tr>
<td>Username</td>
<td>
<input type="text" name="username" required="required">
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="password" required="required">
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" value="Login">
</td>
</tr>
</table>
<?= form_close() ?>
</body>
</html>
Success View
In demo folder, create new PHP file named success.php as below:
<html>
<head>
<title>Login Form in CodeIgniter 4</title>
</head>
<body>
Welcome <?= session('username') ?>
<br>
<a href="<?= site_url('demo/logout') ?>">Logout</a>
</body>
</html>
Define Routes
Open Routes.php file in app/Config folder. Set default controller as below:
$routes->get('/', 'Demo::index');
$routes->get('/demo/welcome', 'Demo::welcome');
$routes->get('/demo/logout', 'Demo::logout');
$routes->post('/demo/process_login', 'Demo::process_login');
Structure of CodeIgniter 4 Project
Run Application
Access index action in Demo controller with following url: http://localhost:8091/LearnCodeIgniterWithRealApps/demo/index
Output
Test with invalid account is username: abc and password: 123
Output
Test with valid account is username: pmk and password: lab.
Output
Click logout link from success page to remove session and open login page again
Output