How to build CodeIgniter URL with hierarchy? How to build CodeIgniter URL with hierarchy? codeigniter codeigniter

How to build CodeIgniter URL with hierarchy?


There are 3 options, sorted by how practical they can be:

  • Use URI Routing. Define a regular expression that will use a specific controller/method combination for each URL.

Something like that could help you, in routes.php:

$route['project/'] = 'project/viewall';$route['project/(.+)'] = 'project/view/$1';$route['project/(.+)/item/'] = 'project/view/$1';$route['project/(.+)/item/(.+)'] = 'item/view/$2';

That is, considering your controllers are item and project respectively. Also note that $n in the value corresponds to the part matched in the n-th parenthesis.

  • Use the same controller with (or without) redirection. I guess you already considered this.
  • Use redirection at a lower level, such as ModRewrite on Apache servers. You could come up with a rule similar to the one in routes.php. If you are already using such a method, it wouldn't be a bad idea to use that, but only if you are already using such a thing, and preferably, in the case of Apache, in the server configuration rather than an .htaccess file.


You can control all of these options using routes.php (found in the config folder). You can alternatively catch any of your URI segments using the URI class, as in $this->uri->segment(2). That is if you have the URL helper loaded. That you can load by default in the autoload.php file (also in the config folder).