CodeIgniter 4 Route Group Tutorial | Route Grouping

Share this Article
Reading Time: 5 minutes
10,630 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 –

Let’s get 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.


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

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5