I’ve seen a lot of people requesting help on figuring out routing on CodeIgniter (2.1.X as of this entry). Which leads me to believe they don’t read the guide and confuse how routes are setup.
So, Here is our scenario, We want to have a nice clean URL of:
http://example.com/account/manage/4123245/jakub
Up to this point, we are fine, we don’t panic, we say “Self, we can do this”. Then we confuse ourselves by organizing our controllers and putting them into sub directories (or folders if you will) and our controller location looks like so:
/application/controllers/account/manage.php
So what’s the problem here? Well none really, you are fine in organizing your controller in subdirectories if you want. However you have to map your routes to those controllers so that CI understands EXACTLY what route you want to take (to which controller etc;)
Analysis
So lets review the URI that we want, we currently have:
- account – this, doesn’t mean anything (it looks like a folder, but isn’t! it would be considered controller actually unless you setup the proper route, this is the problem people always create, they confuse folders and controllers and routing fails to work.
- manage – a controller (with only an index() function)
- 4123245 – a numeric value (input)
- jakub – a string value (just extra input)
By default, CodeIgniter will NOT know that you have a controller in a folder, it is designed to handle the following format:
/controller/method/value/
Understand your route
Knowing that, we need to make sure that CodeIgniter understands our URI route, lets edit our /application/config/routes.php file in order to fix this route problem.
$route['account/manage/(:num)/(:any)'] = "account/manage/index/$1/$2";
Upon seeing that $route most people seem to forget everything here. You have to remember that the value on the LEFT is the INCOMING request, while the RIGHT value (of the equals) is the ROUTE TRANSLATION. If we walk through it, it means:
- Any URI that has a format of /account/manage/(:a number)/(:anything here)
- Need to load the following controller in folder /account/manage
- and finally the remaining piece after the controller /account/manage/index/$1/$2 which means load the index() function and pass it two values
Our simple manage.php controller looks something like this:
<?php
class Manage extends CI_Controller {
public function index($id, $username)
{
// do something here with the two values
}
}
?>
Conclusion
At this point it should be clear, we route the REQUEST to the correct controller, and our app works as intended. The values $1, $2 are just input (1st, 2nd respectively) to show it goes to the index class.
Important: Do not use leading/trailing slashes.
There you have it, no more route issues with folders in your controller structure.
Happy Code Igniting!
having trouble with this
does this require setting up an htaccess file?
Might want to post the actual ‘trouble’ you are having, so that I could possibly suggest a solution.
This has nothing to do with .htaccess appart from setting up your initial .htaccess file for clean urls, this is routing, as in from one place to another.
i’ve got problem when i want use routing and pagination, too
my routing:
$route[‘forum/(:num)/page/(:num)’] = “forum/id/$1/page/$2”;
it works until i want to go back from page 2 to page 1…in codeigniter if we go back to the first page in url it shows no number…in my opinion:
page 2 -> …./forum/2/page/2 it works
page 1 -> …./forum/2/page/ and here is the problem…it shows 404 Page Not Found
WHY? (sry for my bad english 🙂 )
Peter, why not simply add an additional static route for your ‘page 1’ issue:
$route[‘forum/(:num)/page/’] = “forum/id/$1/page/1”;
You basically account for the page 1, OR you would modify your link / url scheme in your pagination setup.
i was trying to put there route to page1
$route[‘forum/(:num)/page/’] = “forum/id/$1/page/1”;
but it does not solved the problem.. 🙁 maybe i just delete from url “page”, but after that the url will be ugly
what if in the slug we have a string containing spaces e.g slug = this is new 1.
when i used this slug without space it works fine
but when with spaces then it says page not found. plz help, i am new to CI
You cannot use spaces in a URI, this is basic HTML URI 1on1. If you force it, it will not work. You may use CI’s url_title() to convert strings with spaces into strings that are SAFE for the URI
Thanks for this. It’s just what I was looking for. I’m pretty new to CI, and like to think I catch on pretty quick to most things. This had me pulling my hair out.
in my controllers I have two directories, pub/ and dashboard/
in my router.php I just needed to do $route[‘dashboard/(:any)’] = ‘dashboard/$1’; to forward into the dashboard/ directory and then follow URI’s as intended.
Thanks again, much appreciated.
folder in application/controller/subfolder/class1/functiona
This is not working if functiona() or home() etc is difne. its works only if index() function is defined.
we will not define router for all function. what is smart solution ?
what will be the solution, because its really hard to define all routes.php because i have so many links.
inside application/controller/mobile, i have a directory mobile inside the controller. In mobile controller i have make a controller home.php . in home.php i have function home() and grade(). how i call home() and grade() without defining any URL in routes.php?
Well, all of my code works. These are examples, you’ll need to cganhe things according to your needs. Maybe you don’t have CodeIgniter installed, maybe you aren’t using ExtJS 3.0 I don’t know. There is a lot of documentation available on both framework sites to help you with this.