Jump to content

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'



Link to comment
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.

Edited by CPD
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.