How To Use A Sub Folder In Default Controller Route In Codeigniter 3

Introduction

By default, CodeIgniter 3 expects the default controller to be located directly inside the controllers directory.

This guide explains how to modify the CodeIgniter 3 Router so that the default controller can be placed inside a subfolder within the controllers directory.

Prerequisites

Before starting, make sure:

  • CodeIgniter 3 is installed.
  • You have access to the application source code.
  • You have permission to modify the CodeIgniter system files.
  • A controller is available inside a subfolder under application/controllers.

Implementation

Step 1: Open the Router.php File

Open the following file:

application/system/Router.php

Locate the mentioned section of the file.

Step 2: Modify the Default Controller Parsing

Around line 298, locate:

if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)

Replace it with:

if (!sscanf($this->default_controller, '%[^/]/%[^/]/%s', $directory, $class, $method) !== 2)

This modification allows the default controller route to include a subfolder.

Step 3: Modify the Controller File Path

Around line 303, locate:

if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))

Replace it with:

if ( ! file_exists(APPPATH.'controllers'. DIRECTORY_SEPARATOR . $directory. DIRECTORY_SEPARATOR .ucfirst($class).'.php'))

This changes the controller path check so that CodeIgniter can locate the controller inside the specified subfolder.

Step 4: Set the Controller Directory

Before:

$this->set_class($class);

add the following line:

$this->set_directory($directory);

The modified section should therefore set the directory before setting the controller class.

Step 5: Configure the Default Controller

Once the Router changes are made, configure the default controller value with the required directory structure.

For example, if your controller is located at:

application/controllers/admin/Home.php

the default controller can be configured using the subfolder and controller name.

Conclusion

CodeIgniter 3 normally expects the default controller to be directly under the controllers directory. By modifying the Router configuration, you can allow the default controller to reside inside a subfolder.

This is useful when organizing controllers into separate directories, especially for larger CodeIgniter applications.

FAQs

1. Where should the Router file be modified?

Open:

application/system/Router.php

and update the specified sections.

2. Why use a subfolder for controllers?

Subfolders help organize controllers into logical groups, particularly in larger applications.

3. Which method is used to set the controller directory?

Use:

$this->set_directory($directory);

before setting the controller class.

4. Does this change the CodeIgniter core files?

Yes. This approach modifies Router.php, which is part of the CodeIgniter system files. Keep a backup of the original file before making changes.

Send Email Using HTML Templates in CodeIgniter

Learn how to send emails using HTML templates in a CodeIgniter application.

https://pheonixsolutions.com/blog/send-email-using-html-templates-codeigniter/

Talk to our experts

Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here.

admin

Writes about Security at Pheonix Solutions.

Leave a Reply

Scroll to Top