How To Use Namespace Routes in CodeIgniter 4

Share this Article
Reading Time: 4 minutes

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 –

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s gets started.

Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Turn Development Mode On

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

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.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# 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.


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.


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.

Buy Me a Coffee

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.

Find More on CodeIgniter 4 here