Jump to content


This topic is now archived and is closed to further replies.


Codeigniter Routes Problem

Recommended Posts

Happy New Year everyone!

OK so I am sure my problem is easily resolved, but it isn’t obvious to me so maybe one of you experts can help.

I have a controller called products, with a bunch of methods, index shows all products by category, there is a method azproducts does the same thing but groups by letter, then i have a search method also. then each product has its individual view in the view method.

here are my routes:

$route[‘products’] = ‘products’;

$route[‘products/addtobasket/(:any)’] = ‘products/addtobasket/$1’;

$route[‘products/view/(:any)’] = ‘products/view/$1’;

$route[‘products/azproducts/(:any)’] = ‘products/azproducts/$1’;

$route[‘products/search/(:any)’] = ‘products/search/$1’;

$route[‘products/(:any)’] = ‘products/index/$1’;

Now I think the issue is to do with the bottom line, as if I remove ‘index’ from $route[‘products/(:any)’] = ‘products/index/$1’ it returns with a 404, but if i leave it in, it works. However with it left in, the search page then fails to work and routes to the index method instead. does anyone have any ideas what I am doing wrong?


I have posted this also on EllisLabs Codeigniter forum, but I think this site has better 'footfall'



Share this post

Link to post
Share on other sites

Are you just going to /products/search when searching?

Share this post

Link to post
Share on other sites

I don't know how the router is set up, but I suspect it has something to do with the /(:any) bit not being optional. Worth having a look at, at least.

Also, what happens if you put the problematic line on top?

Share this post

Link to post
Share on other sites

You don't need to define every possible link e.g.


$route['products/view/(:any)'] = 'products/view/$1';


Provided there is a controller called Products and a method called view it will get executed as this is the default routing method - which can be reconfigured if you wish - and any arguments will be passed as parameters. Secondly, when defining routes it'll take the last defined route and overwrite any conflicting routes.


In your case you have:


$route['products/search/(:any)'] = 'products/search/$1';
$route['products/(:any)'] = 'products/index/$1';


CodeIgniter sees the second one and re-routes anything with "products" in to products/index/$1 i.e. every single method you've defined will now be redirected to your index method


Taking into account my previous comments, remove both.


Edit: Apologies for all the edits, spotted a load of grammar and language errors.

Share this post

Link to post
Share on other sites

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.