Reverse-Routing with Controller Actions
Reverse-routing is generating URLs based on route declarations.
For example the following route declaration tells Laravel to execute the action "register" in the controller "users" when the request's URI is 'register'.
// http://mysite.com/register
Route::any('register', 'users@register');
Traditionally, we may link to the registration page like this:
{{ HTML::link('register', 'Register Now!') }}
However, this has the unfortunate disadvantage of being dependent on our route declaration. If we change the route declaration to:
// http://mysite.com/signup
Route::any('signup', 'users@register');
Then our link will be wrong. We'll have to go throughout our entire site and fix our links. Hope we don't miss one!
Instead, let's use reverse-routing.
{{ HTML::link_to_action('users@register', 'Register Now!') }}
Now, the link that we generate will automatically change when we change our routing table. In our first example it'd generate http://mysite.com/register. Then, when we change the routes call to match our second example it'll generate http://mysite.com/signup.
Reverse-routing can make your app not only more robust but easier to maintain. You're often more familiar with your controllers and actions than your URLs.
Your Goal for the Day
Use URL::to_action(), Redirect::to_action(), or HTML::link_to_action() at least one time today. Implement it, change a route, and see how it reacts.
Required Reading
Official Documentation
Laravel's URL class' to_action() method
Laravel's HTML class' link_to_action() method
Laravel's Redirect class' to_action() method
Additional Resources
Forum Post on Routing to Closures and Controller Actions as well as Reverse-Routing best-practices.
Screenr: Laravel 3 Controller Routing & Reverse Routing












