Welcome Guest, Not a member yet? Register   Sign In
DrF Reverse Routing
#3

[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{
   function view( $productId )
   {
      // do stuff here
   }
}

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' );
// which provides http://www.example.com/products/view/3

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
$route['products/(:num)'] = 'products/view/$1';

...

// in some view
echo anchor( 'products/3', 'View Product' );
// which creates a link to http://www.example.com/products/3

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
$route['products/(:num)'] = 'products/view/$1';

...

// in some view
echo anchor( 'products/view/3', 'View Product' );
// which provides http://www.example.com/products/3

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
$route['whatever/you/want/(:num)'] = 'products/view/$1';

...

// in some view
echo anchor( 'products/view/3', 'View Product' );
// which creates a link to http://www.example.com/whatever/you/want/3

Did that clear things up for you?


Messages In This Thread
DrF Reverse Routing - by El Forum - 05-22-2008, 06:12 PM
DrF Reverse Routing - by El Forum - 05-23-2008, 01:54 AM
DrF Reverse Routing - by El Forum - 05-23-2008, 02:33 AM
DrF Reverse Routing - by El Forum - 05-23-2008, 03:13 AM
DrF Reverse Routing - by El Forum - 05-23-2008, 03:54 AM
DrF Reverse Routing - by El Forum - 05-23-2008, 03:55 PM
DrF Reverse Routing - by El Forum - 05-23-2008, 04:53 PM
DrF Reverse Routing - by El Forum - 02-21-2009, 11:38 AM
DrF Reverse Routing - by El Forum - 03-18-2009, 03:52 PM
DrF Reverse Routing - by El Forum - 03-24-2009, 06:09 AM
DrF Reverse Routing - by El Forum - 03-31-2009, 06:15 PM
DrF Reverse Routing - by El Forum - 04-03-2009, 04:50 AM
DrF Reverse Routing - by El Forum - 04-03-2009, 03:44 PM
DrF Reverse Routing - by El Forum - 04-14-2009, 06:05 AM
DrF Reverse Routing - by El Forum - 04-16-2009, 10:50 AM
DrF Reverse Routing - by El Forum - 11-25-2009, 04:41 AM
DrF Reverse Routing - by El Forum - 11-25-2009, 10:26 AM
DrF Reverse Routing - by El Forum - 11-26-2009, 03:55 PM
DrF Reverse Routing - by El Forum - 09-01-2010, 01:27 PM
DrF Reverse Routing - by El Forum - 09-01-2010, 04:17 PM
DrF Reverse Routing - by El Forum - 09-01-2010, 04:49 PM
DrF Reverse Routing - by El Forum - 09-02-2010, 02:57 AM
DrF Reverse Routing - by El Forum - 09-02-2010, 07:49 AM
DrF Reverse Routing - by El Forum - 09-16-2010, 09:54 PM



Theme © iAndrew 2016 - Forum software by © MyBB