Welcome Guest, Not a member yet? Register   Sign In
Problem with routes
#1

[eluser]Unknown[/eluser]
Hi, I have some problems with setting routes.
I need to "ignore" the first segment, for example if the url is one/two/three/four it would be routed to two/three/four. (two = controller, three = function, four = parameter).
I tried using
Code:
$route['(:any)/(:any)'] = '$2';
but it only worked if the url is like one/two and it routes to two.
Shouldn't it work for all urls like: first_segment/many/many/other/segments/ and route this to many/many/other/segments?
if I use
Code:
$route['one/(:any)'] = '$1';
it works for both: one/two and one/two/three/four, it ignores the word "one" if it is there and routes to everything that is after one/ not only the first segment after one.

Writing 'one/(:any)' isn't good for me because I need the first part to be dynamic, thanks!
#2

[eluser]pickupman[/eluser]
You can stack routes, meaning that you put them in a certain order. Routes work by going by first match found. Once a matching route is found, it will not look any further.

Given your example, you may want to use .htaccess to ignore the first segment. Any particular reason for wanting to always ignore the first segment?

How many controllers do you have that you need to be dynamic? Reason being say you have 5 controllers, it would take less code to hardcode the 5 controller routes than creating some loop or querying the DB for them.

Also be aware you may be creating a name collusion problem by always ignoring the first segment.
#3

[eluser]Unknown[/eluser]
I know about the stacking, the problem is using (:any)/(:any) = $2 if the url is one/two/three the match is found but it doesn't route to two/three, just shows 404 (I have controller two with function three). If I write (:any)/(:any)/(:any) = $2/$3 it forks for one/two/three and is routed to two/three.
Also I noticed if I put:
Code:
$route['(:any)/(:any)'] = '$2';
$route['(:any)/(:any)/(:any)'] = '$2/$3';
And the url is one/two/three, the match is found in the first line and as I said above shows 404, but if it is:
Code:
$route['(:any)/(:any)/(:any)'] = '$2/$3';
$route['(:any)/(:any)'] = '$2';
and again one/two/three the match is found in the first line and I'm routed to two/three.
Shouldn't the second "any" reference to all of what is left not just the first segment?
I could just write like 7 lines starting with 8x(:any) and to the last one with 2x(:any), then I could have max 7 segments (1 for controller, 1 for function and 5 for parameters) and if I would need more I could just add extra line but I thought it could be done in a more "appealing" way, I've spent like 2 days to get some alternative, maybe its not worth the effort...

I tried using .htaccess too but I guess I suck at it, I wrote:
Code:
RewriteRule (.*)/(.*) index.php?/$2
, idk what's wrong there but url one/two is interpreted like index.php?/one, but while testing I tried:
Code:
RewriteRule (.*)/(.*) index.php?/$1/smth/$2
and here one/two => index.php?/one/smth/two, so $1=one and $2=two, but in the previous one $2=one

I'm creating something like little blogging system, and the extra segment is for users blog name, for example user A has a blog "hello" and user B has a blog "world", user A would go to example.com/hello to see his blog and user B would go to example.com/world, but they would use the same CI controllers, for example: example.com/hello/comment/add/123 would add a comment to A users blog post with id 123, but example.com/world/comment/add/456 would add a comment to B users blog.

I don't know how many controllers I will have, just started to work on it Smile
Yeah I'm aware about name collision, I'm thinking about using hooks, extending ci_controller or something like that to check if there is a blog with that name in the db.
Well anyway, thank you for your response Smile
#4

[eluser]CroNiX[/eluser]
Try using (.*) in your routes instead of (:any).
Code:
$route['(.*)/(.*)'] = '$2';
That will load the index method of whatever controller $2 is, assuming the controller exists.
#5

[eluser]pickupman[/eluser]
Since the database may not yet be loaded when the routes are parsed, alternate solution would be to cache the user/blog names to a cache file. Then in the routes file, load your cache of usernames. Loop through the array and build the routes dynamically that way.




Theme © iAndrew 2016 - Forum software by © MyBB