DrF Reverse Routing |
[eluser]drfloob[/eluser]
I had been working with CakePHP 1.2 for the last month or so, and I've never had to work so hard to find good documentation. I'd mostly be fine with cake if their code were legible ... but that's another problem entirely ;-) So, here I am, and I'm glad I made the jump. So far, the only thing I have missed from Cake is Reverse Routing: the ability to translate an "absolute" URL into a custom route at runtime. This saves us the headache of hard-coding custom routes into links and redirects, allowing us to redefine routes without worrying about what we're going to break. You can define links and redirects absolutely, using a specific Controller/Function and trust that the URL would contain whatever custom route is setup at the time. I searched the Wiki for projects that tackled reverse routing and didn't find anything. I didn't find anyone asking about it in the forums, either. So, I decided I'd solve this issue myself. The code is in the Wiki at DrF Reverse Routing I am still new to CI (just started yesterday, actually), so it'd be great to get some feedback. Constructive criticism is welcome. I'd especially like to know if someone is already working on a project like this, so we could combine efforts. Thanks,
[eluser]xwero[/eluser]
could you add a real world example because from what i understand from the wiki you have to redirect to another function?
[eluser]drfloob[/eluser]
Well, aside from redirection, reverse routes really shine when creating anchors. Let's say you've got a site with a shopping cart. You've got a Products Controller with a view function that takes a single paramter: $productId. Code: class Products extends Controller{ A normal link for you would be 'products/view/3'. If you don't setup any custom routes, your link code looks like: Code: echo anchor( 'products/view/3', 'View Product' ); Now, you decide to setup a custom route so 'products/(:num)' is tied to 'products/view/$1'. As I understand it, in every url and redirect, you must hard code that custom route. All your anchors now look like Code: // in config/routes.php So here's the BIG issue. What happens if you've got a few hundred of those links scattered around your codebase, and you / your boss / your employee decides the routes need to be changed? You're stuck with changing a few hundred links since the route was so tightly coupled to anchor and redirect code. Hopefully, if only for SEO reasons, you wont ever HAVE to change your routes, but if you do, it's a massive headache. By using reverse routing, your anchors are setup "absolutely". You specify your anchors and redirects as if you didn't have any routes setup, but the resulting URL shows the custom route you setup. With DrF Reverse Routing installed, your anchor would look like: Code: // in config/routes.php Now you're free to do whatever you want with your routes, without worrying about what's going to break. Change your routes, not your code. You can go nuts: Code: // in config/routes.php Did that clear things up for you?
[eluser]xwero[/eluser]
Wow that is an elaborate example, thank you for your time. I can see the benefit in having to change the routes instead of the code but from a SEO point of view the redirect function should have a 301 error response in case some one has saved the old link. This means the helper in only usable from 1.6.2 and up, that is a choice i leave up to you.
[eluser]drfloob[/eluser]
You're welcome. It's good to have someone to discuss this stuff with :-) Reverse Routing doesn't attempt to deal with how redirects are handled, just how uri's are translated. You're right though, a 301 redirect would be necessary if you're changing a well-established url. However, the ways that legacy routes are handled (or not) are probably a lot more personal and varying than this general concept, so I would leave it to the individual developer to decide their own preference. Off the top of my head, if you want legacy routes to be redirected with a 301, I suppose you could redirect old routes to a "LegacyRoute" controller with some url translation and 301 redirect magic ... or any number of other solutions. The url helper's set_header function goes back to CI v1.4.1, so it's possible to do a 301 redirect up to about 2 years ago, but yeah, v1.6.2 definitely made it more convenient. Whether anything else breaks in CI version < 1.6.2 is beyond me ;-)
[eluser]nmweb[/eluser]
CI lacked this feature. I've been trying to implement something similar but your thing is neat. Are you going to add stuff like :year etc. there's some real cool stuff for reverse routing.
[eluser]drfloob[/eluser]
Thanks, nmweb. Right now, DrFRR is reverse compatible with normal routes. Add DrFRR into your project, take it out, things wont break either way. That's what's cool about using the CI native "absolute" routes. If we start defining custom regex in the site_url function, we lose reverse compatibility. If you take DrFRR out of your project, CI's native routes have no idea what :year and :month mean anymore. But, you could accomplish the same thing in your routes.php file with something like: Code: $year = '[12][0-9]{3}';
[eluser]Johan André[/eluser]
I use this for alot of projects... It's great. Should be included in the core!
[eluser]ElToro[/eluser]
Thank you very much, this is great! I just stated to integrate this in to my project... Done. Has anyone figured out howto vary routes depending on language? I am not doing multilanguage site yet, but it has crossed my mind...
[eluser]Johan André[/eluser]
I just noticed one thing: Let's say you have the following in routes.php: Code: $route['(section_1|section_2)/gallery/(:any)'] = "gallery/show/$2/$1"; This following: Code: <?=anchor('gallery/1/section_1', 'Link to gallery');?> Should (reverse) rewrite to: Code: <a href="http://www.mysite.com/section_1/gallery/1">Link to gallery</a> But it does not. Probably because I used $2 before $1 in the route. Is this an known error? Regards |
Welcome Guest, Not a member yet? Register Sign In |