Organizing Codeigniter Controllers into Sub-Directories or Subfolders

While developing large applications, we might wish to organize Controllers into Sub-Directories or Subfolders for our convenient. This article will guide you to smoothly organize the controllers in sub-directory or folder. I’m using Codeigniter version 3.0 here.

First of all create a sub-directory as you wish inside your application > controllers directory and create a controller file in newly created subfolder as you usually do. For version 3.0, the controller class and file name must be appear as first letter with capital case. For example let’s say we have created a folder named ‘admin‘ and ‘Home.php‘ as default controller.

Specify routing rule to organize Controller into sub-directory

Now head to application > config > routes.php and create a new rule to let codeigniter understand the URL and load the correct controller. So for our URL write the following routing rule:

Now access the URL and page will show as whatever you have written in controller file to render view. But wait, what if you have functions & parameters in URL for admin? Let us specify few more rules to match with our controller functions:

I have written matching routing rules and explained above as well in the code. Please be careful about the orders of rules because once a rule is matched all the later rules (even if they are correct) are discarded. As you can see in code below:

You must have to give it a chance to match ‘manager’ before matching ‘any’. otherwise as per wrong order it will always hit ‘any‘ and redirect to ‘frontend’. It all about Organizing Codeigniter Controllers into Sub-Directories or Subfolders. Did you feel any difficulty or have any suggestion or ask something then comment below.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.