CodeIgniter 4 Route Group Tutorial | Route Grouping

Share this Article
Reading Time: 5 minutes
4,345 Views

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

Group simply means collection. So here, we will have collection of routes which in terms called Route Group. We will see the concept of namespace in route group as well. 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 get 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 Controller & Route Group

Open project into terminal and run this command.

$ php spark make:controller Device --suffix

This command will create a file called DeviceController.php at location /app/Controllers folder.

Let’s add few lines of code into it. Right now code is not the focus point, we need to see How can we create a route group.

Open DeviceController.php and write this code.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class DeviceController extends BaseController
{
	public function index()
	{
		// .. code here
	}

	public function addDevice()
	{
		if($this->request->getMethod() == "post"){
			//.. post submit code
		}
		// .. code here
	}
}

We have few sample methods inside controller.

Let’s configure very simple routes without using route group concept.

Routes without Group concept

Open Routes.php from /app/Config folder.

//...

# Add these routes
$routes->get("device", "DeviceController::index");

$routes->get("device/add-device", "DeviceController::addDevice");
$routes->post("device/add-device", "DeviceController::addDevice");

OR
  
$routes->match(["get", "post"], "device/add-device", "DeviceController::addDevice");

So, here we can observe one thing. In each route we have a common prefix i.e device with each route.

Routes with Group Concept

//...

/*route group*/
$routes->group("device", function($routes){

    // URL - /device
    $routes->get("/", "DeviceController::index");
  
    // URL - /device/add-device
    $routes->match(["get", "post"], "add-device", "DeviceController::addDevice");
});

It will work with the same concept what we have seen above.

Let’s use this route group concept for namespace controller routes.


Controller with Namespace & Route Group

Open project into terminal and run this command.

$ php spark make:controller Admin\\Admin --suffix

It will create a file called AdminController.php inside /app/Controllers/Admin folder. Initially we don’t have any Admin folder. But when we run above command it will create automatically.

Where we should this concept?

Suppose we have an application where panels are Admin & Site. We want the separate folders where we will do code for Admin and Site. This concept is useful in that case.

Open AdminController.php and write this following code into it.

<?php

namespace App\Controllers\Admin; // Controller namespace

use App\Controllers\BaseController;

class AdminController extends BaseController
{
	public function index()
	{
		// .. code here
	}
  
    public function addUser()
	{
		if($this->request->getMethod() == "post"){
			//.. post submit code
		}
		// .. code here
	}
}

Routes Configuration with Group

Open Routes.php from /app/Config.

//...

$routes->group("admin", ["namespace" => "App\Controllers\Admin"], function($routes){

    // URL - /admin
    $routes->get("/", "AdminController::index");
    // URL - /admin/add-user
    $routes->match(["get", "post"], "add-user", "AdminController::addUser");
});

Route Group with Filter

Suppose in an application we have filters. Let’s say to check “authentication”.

To add filters with routes – Without having route group

//...

$routes->get("admin/route-1", "AnyController::method1", ["filter" => "auth"]);
$routes->get("admin/route-2", "AnyController::method2", ["filter" => "auth"]);
$routes->get("admin/route-3", "AnyController::method3", ["filter" => "auth"]);

Now, applying filters with route group.

//...

$routes->group("admin", ["filter" => "auth"], function($routes){

    $routes->get("route-1", "AnyController::method1");
    $routes->get("route-2", "AnyController::method2");
    $routes->get("route-3", "AnyController::method3");
});

So, these are the methods we can use to create a route group in codeigniter 4.

We hope this article helped you to learn CodeIgniter 4 Route Group Tutorial in a very detailed way.

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