Routes in 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 36 remove index.php string in $indexPage variable as below:

public $indexPage = '';




Open App.php file in app/Config folder. Set value for $baseURL variable as below:

public $baseURL = 'http://localhost:8095/LearnCodeIgniter4WithRealApps/';

In app/Controllers folder create new controllers as below:

Create new PHP file named Demo.php in app/Controllers folder as below:

<?php

namespace App\Controllers;

use App\Models\Product;

class Demo extends BaseController
{
	public function index()
	{
		return view('demo/index');
	}

	public function index2()
	{
		return view('demo/index2');
	}

	public function index2_post()
	{
		return view('demo/index2');
	}

	public function index3($id)
	{
		$data['id'] = $id;
		return view('demo/index3', $data);
	}

	public function index4($id1, $id2)
	{
		$data['id1'] = $id1;
		$data['id2'] = $id2;
		return view('demo/index4', $data);
	}
}

In app/Controllers, create new folder named Admin. In Admin folder, create new PHP file named Product.php as below:

<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;

class Product extends BaseController
{
    public function findAll()
    {
        return view('admin/product/findAll');
    }

    public function find()
    {
        return view('admin/product/find');
    }

    public function create()
    {
        return view('admin/product/create');
    }
}




in app/Views folder, create new views as below:

Create new folder named demo in app/Views folder. In demo folder, create new views as below:

Create new PHP file named index.php as below:

<html>

<head>
    <title>Index Page</title>
</head>

<body>

    <fieldset>
        <legend>Demo Controller Routing</legend>
        <a href="<?php echo site_url('demo/action1'); ?>">Action 1</a>
        <br>
        <a href="<?php echo site_url('demo/action2'); ?>">Action 2</a>
        <br>
        <form method="post" action="<?php echo site_url('demo/action2'); ?>">
            <input type="submit" value="Submit to Action 2">
        </form>
        <a href="<?php echo site_url('demo/action3/123'); ?>">Action 3</a>
        <br>
        <a href="<?php echo site_url('demo/action4/123/p01'); ?>">Action 4</a>
    </fieldset>

    <fieldset>
        <legend>Product Controller Routing</legend>
        <a href="<?php echo site_url('admin/product/findall'); ?>">Find All</a>
        <br>
        <a href="<?php echo site_url('admin/product/find/123'); ?>">Find</a>
        <br>
        <form method="post" action="<?php echo site_url('admin/product/create'); ?>">
            <input type="submit" value="Create new product">
        </form>
    </fieldset>

</body>

</html>

Create new PHP file named index.php as below:

<html>
	<head>
		<title>Index 2 Page</title>
	</head>
	<body>

		<h3>Index 2 View</h3>

	</body>
</html>

Create new PHP file named index3.php as below:

<html>

<head>
    <title>Index 3 Page</title>
</head>

<body>

    <h3>Index 3 View</h3>
    Id: <?= $id ?>

</body>

</html>




Create new PHP file named index4.php as below:

<html>

<head>
    <title>Index 4 Page</title>
</head>

<body>

    <h3>Index 4 View</h3>
    Id1: <?= $id1 ?>
    <br>
    Id2: <?= $id2 ?>

</body>

</html>

Create new folder named admin in app/Views folder. In admin folder, create new views as below:

Create new PHP file named find.php as below:

<html>

<head>
    <title>Index Page</title>
</head>

<body>

    <h3>Find Product - Admin Panel</h3>
    Id: <?= $id ?>

</body>

</html>

Create new PHP file named findAll.php as below:

<html>

<head>
    <title>Index Page</title>
</head>

<body>

    <h3>Find All Product - Admin Panel</h3>

</body>

</html>

Create new PHP file named create.php as below:

<html>

<head>
    <title>Index Page</title>
</head>

<body>

    <h3>Create New Product - Admin Panel</h3>

</body>

</html>




Open Routes.php file in app/Config declare Route Definitions as below:

$routes->get('/', 'Demo::index');
$routes->get('/demo/action1', 'Demo::index');
$routes->get('/demo/action2', 'Demo::index2');
$routes->post('/demo/action2', 'Demo::index2_post');
$routes->get('/demo/action3/(:any)', 'Demo::index3/$1');
$routes->get('/demo/action4/(:any)/(:any)', 'Demo::index4/$1/$2');

$routes->group('demo', function ($routes) {
    $routes->get('/', 'Demo::index');
	$routes->get('action1', 'Demo::index');
    $routes->get('action2', 'Demo::index2');
    $routes->post('action2', 'Demo::index2_post');
    $routes->get('action3/(:any)', 'Demo::index3/$1');
    $routes->get('action4/(:any)/(:any)', 'Demo::index4/$1/$2');
});

$routes->group('admin', function ($routes) {
	$routes->group('product', function ($routes) {
		$routes->get('findall', 'Admin\Product::findAll');
		$routes->get('find/(:any)', 'Admin\Product::find/$1');
		$routes->post('create', 'Admin\Product::create');
	});
});




Access index action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/index

Output

Click Action 1 link will call to index action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/action1

Output

Click Action 2 link will call to index2 action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/action2

Output

Click Submit to Action 2 button will submit form with POST method to index2_post action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/action2

Output




Click Action 3 link will call to index3 action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/action3/123

Output

Click Action 4 link will call to index4 action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/action4/123/p01

Output

Click Find All link will call to findAll action in Product controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/admin/product/findall

Output

Click Find All link will call to findAll action in Product controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/admin/product/findall

Output

Click Find link will call to find action in Product controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/admin/product/find/123

Output

Click Create new product button will submit form with POST method to create action in Product controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/admin/product/create

Output