CodeIgniter Forums
Improve routes rule help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Improve routes rule help (/thread-29750.html)



Improve routes rule help - El Forum - 04-20-2010

[eluser]chefnelone[/eluser]
Hello

I learnign how to use route[] in the config/routes.php file;

I've done this:

Code:
$route['shop/:any/:num/:num/:num'] = 'shop/detail/$3/$4/$5';
//then: 'shop/pizza/203/0/4' is routed to 'shop/detail/203/0/4'

which works fine.

But I'like to get rid of the last 3 segments (/203/0/4) Problem is that I need to pass those segments to the 'detail' function.

Is there any way to do this (maybe with .htaccess)


Improve routes rule help - El Forum - 04-20-2010

[eluser]mddd[/eluser]
If you need those numbers to find the correct detail, then it seems to me you can't get rid of them.
Can you give an example of what you want to achieve?


Improve routes rule help - El Forum - 04-20-2010

[eluser]chefnelone[/eluser]
let's say:
I have an anchor like: <a href="shop/pizza/203/0/4">go</a>

Once you click it, it is routed to 'shop/detail/203/0/4' BUT in the browser address bar is just shown: 'shop/pizza' (instead of shop/pizza/203/0/4).

The idea is to send the parameters but no show then in the browser address bar.


Improve routes rule help - El Forum - 04-20-2010

[eluser]mddd[/eluser]
Ok, I get it. But you do realise that the user will then not be able to link to that page, or bookmark it!
Because if the user bookmarks it, it will only say '/shop/pizza' and if the user comes back to that page, your site will not be able to produce that page again!

If you still want to do it, you could make a controller that stores the information in a session and then redirects the browser to your '/shop/pizza' page. On that page you read the information from the session and you're done.

But I think it's the wrong way to go. It is a bad idea to try to show the user something else than the real url. You'd much better make sure that the real url contains something useful. For instance, if '203' is the id of some pizza shop, you could make your route to /shop/pizza/203/my-great-pizza-shop.
You can read the id you need from the url (203) and the url is human-readable as well. That will make the url logical for a user.
You could even use /shop/pizza/my-great-pizza-shop, but then you would have to make sure that there are no shops with the same name.


Improve routes rule help - El Forum - 04-20-2010

[eluser]chefnelone[/eluser]
You right, I've got it. I'll try with your "/shop/pizza/203/my-great-pizza-shop" idea.
thanks