How To Use Namespace Routes in CodeIgniter 4

Reading Time: 4 minutes
11,006 Views

Already we have an article over CodeIgniter 4 Routing. Inside this article we will create and see CodeIgniter 4 Namespace route concept. This tutorial is very interesting to see and learn.

Namespace simply means placement of controllers into different different folder hierarchy and linking their routes with application.

Additionally we will see the concept of namespace in route group. In codeigniter 4, these interesting features makes the development too much easier.

Learn More –

Let’s gets started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


Create Namespace Controllers

Next, we will create controllers into different different folder.

Say we have 2 sections in a site namely as admin & user. We will create different different controller for all these.

# Admin Controller
$ php spark make:controller Admin/Admin --suffix

# User Controller
$ php spark make:controller User/User --suffix

These commands will create two folders with name Admin and User inside /app/Controllers folder.

Inside Admin folder, you should see we will have AdminController.php file. Inside User folder, you should see we will have UserController.php file.

Open AdminController.php and write this few lines of code into it.

<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;

class AdminController extends BaseController
{
	public function about()
	{
		// some code
	}

	public function product()
	{
		// some code
	}
}

Open UserController.php from /app/Controllers/User folder and write this few lines of code into it.

<?php

namespace App\Controllers\User;

use App\Controllers\BaseController;

class UserController extends BaseController
{
	public function profile()
	{
		// some code
	}

	public function report()
	{
		// some code
	}
}

Create Namespaced Routes

Open Routes.php file from /app/Config folder. Add these routes into it.

//...

$routes->get("about", "AdminController::about", ["namespace" => "App\Controllers\Admin"]);
$routes->get("product", "AdminController::product", ["namespace" => "App\Controllers\Admin"]);

$routes->get("profile", "UserController::profile", ["namespace" => "App\Controllers\User"]);
$routes->get("report", "UserController::report", ["namespace" => "App\Controllers\User"]);

//...

We can make these routes into a more clear format by using route group.

Using route groups

//...

# Admin Routes
$routes->group("admin", ["namespace" => "App\Controllers\Admin"] , function($routes){
	 
    // URL - /admin/about
    $routes->get("about", "AdminController::about");
    // URL - /admin/product
    $routes->get("product", "AdminController::product");
});

# User Routes
$routes->group("user", ["namespace" => "App\Controllers\User"] , function($routes){

    // URL - /user/profile
    $routes->get("profile", "UserController::profile");
    // URL - /user/report
    $routes->get("report", "UserController::report");
});

//...

We hope this article helped you to learn How To Use Namespace Routes in CodeIgniter 4 in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.