Welcome Guest, Not a member yet? Register   Sign In
Easy route question
#1

[eluser]housecor[/eluser]
http://ellislab.com/codeigniter/user-gui...uting.html says I can do a route like this: $route['blog/joe'] = "blogs/users/34";

and the result will be:

A URL containing the segments blog/joe will be remapped to the "blogs" class and the "users" method. The ID will be set to "34".

How do I access the ID (that's being set to 34 in this example) since it's not in the URL after the route runs?

Thanks in advance!
#2

[eluser]xwero[/eluser]
It's a bad real world example for routing because if you want to do this you should add a route for every user you have. A real world example would be
Code:
$route['blog/([a-zA-Z0-9\-]+)'] = 'blog/users/$1';
It does almost the same but instead of adding an id it grabs the second segment and places it in the default url path.
#3

[eluser]housecor[/eluser]
Thanks! However, it looks like your solution is just taking the parameter after blog and placing it after blog/users.

What I'm trying to translate is:
blog/username to blog/getuser/id

So I need to translate the username in the url to the associated ID. What's the best way to pull that off? I'd prefer feeding my controller the ID in the DB rather than the username.
#4

[eluser]xwero[/eluser]
Like i said then you have to add a route for every user which means you are going to have a routes file from the north pole to the south pole if you have a popular site. The routes.php file is a static file that is why it's a config file. it's a bad real world example.
#5

[eluser]housecor[/eluser]
Ah, agreed! Though my example said user I'm actually trying to do this for my limited category structure:

For example:
Route Products/Shoes -> Products/ShoeCategoryID

I just want "vanity" category level urls. Since I only have around 35 categories using routes would work, but I still don't see how to access the ID like the user guide is saying. And I'd prefer not to hard code my categories in my route config since Categories will change fairly often.

So routes may not be the solution I'm looking for. Any recommendation on how I could pull this off?
#6

[eluser]Pascal Kriete[/eluser]
You *could* do this with routing, but as xwero mentioned - you would have to hardcode each category id, which can be rather impractical.

A better solution might be to remap the controller. Then you can get a category id from the database just by checking the url slug.
#7

[eluser]christian.schorn[/eluser]
If you have a small set of categories, why not make the primary key in the database the URL-friendly version of the category name?

That way "categories/shoes" or "categories/shirts_without_holes" would work with just the default routing.

Edit: It doesn't even have to be the primary key, a unique index is enough.




Theme © iAndrew 2016 - Forum software by © MyBB