issues with Routing

#1
[eluser]Unknown[/eluser]
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?

#2
[eluser]PhilTem[/eluser]
Routes are "first come, first served" thus 'products' => "products" will always match and all the others won't be evaluated.

This one should fix your problem
Code:
$route['products/(addtobasket|view|azproducts|search)/(.+)'] = 'products/$1/$2';
$route['products/(addtobasket|view|azproducts|search)'] = 'products/$1';
$route['products/(.+)'] = 'products/index/$1';
$route['products'] = 'products/index';

And this one should work as well and be much faster
Code:
$route['products/(?!index)/(.+)'] = 'products/$1/$2';
$route['products/(?!index)'] = 'products/$1';

#3
[eluser]Unknown[/eluser]
Many Thanks for that Phil, the first one works perfectly, but the second doesnt quite perform the same, it seems to be not going to the index method. I expect that is probably down to my routes file having some odd entries!! I will look at wwhat oyu have done and try and improve my own routes.

The CI guides don't explain all the little tokens you can use, do you know where there might be better documentation?

does this mean? (.+)

or this bit? (?!index) I am guessing that one means something but not index.


Many thanks for your solution though.

#4
[eluser]pickupman[/eluser]
The special character notations he had given are regular expressions. The are mentioned in the [url="http://ellislab.com/codeigniter/user-guide/general/routing.html"]user guide[/url].
Quote:If you prefer you can use regular expressions to define your routing rules. Any valid regular expression is allowed, as are back-references.

Regular expressions are tricky to learn at first, but are a huge time saver in programming for certain tasks like character/pattern matching.

#5
[eluser]Aken[/eluser]
Your URL scheme isn't that great, IMO. I would consider separating some of your URLs and logic so you don't need to throw everything under your Products controller and have to deal with route nightmares.

You could always consider using the _remap() method to route your call, instead of routes themselves. It would allow you to modify your Products controller at will without needing to update your routes config each time. It's a little harder but can be more effective in some situations.

Phil's first suggestion is the best from a route standpoint, unless you prefer to separate your segments for whichever reason (putting them all together like that has its limitations, but in your desired case it's fine). The first two routes could be consolidated into one, but it makes looking/editing it more confusing for someone who doesn't understand regular expressions. The last route for "products" by itself is redundant - CI will already call the index() method automatically.

Phil, not sure what you're doing with the second suggestion. You're using a negative lookahead, when I think you're trying to say "when this segment is != index". Definitely not the same thing. I also think this kind of route is pointless, when you can do the same functionality with the _remap() method and gain more control over the incoming URL in the process.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.