Table of Contents
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.
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.
Settings Environment Variables
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.
// .. Other routes # 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
// .. Other routes /*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.
// .. Other routes $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
// .. Other routes $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
- CodeIgniter 4 Cookie Helper Tutorial
- CodeIgniter 4 CRUD Application Tutorial
- CodeIgniter 4 CRUD REST APIs Tutorial
- CodeIgniter 4 CSRF Token in AJAX Request
- Database Query in CodeIgniter 4 Tutorial
- CodeIgniter 4 Ajax Form Data Submit
- CodeIgniter 4 Form Validation Tutorial
- CodeIgniter 4 Image Upload with Form Tutorial
- Multi language in CodeIgniter 4 Tutorial
- Stripe Payment Gateway Integration in CodeIgniter 4
- CodeIgniter 4 CSRF Token Tutorial
- CodeIgniter 4 Basics Tutorial
- CodeIgniter 4 Spark CLI Commands Tutorial
- Migration in CodeIgniter 4 Tutorial
- Seeders in CodeIgniter 4 Tutorial
Hi, I am Sanjay the founder of ONLINE WEB TUTOR. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. Total experience of 7+ years in web development. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.