Using named routes
There may be times when we need to change our route's name. On a large site, this could cause a lot of problems if we have multiple links to an incorrect route. Laravel provides an easy-to-use way of assigning names to our routes, so we never have to worry if they change.
Getting ready
For this recipe, we need a standard Laravel installation.
How to do it...
To complete this recipe, follow these steps:
In our
routes.php
file, create a named route as follows:Route::get('main-route', array('as' => 'named', function() { return 'Welcome to ' . URL::current(); }));
Create a route that performs a simple redirect to the named route:
Route::get('redirect', function() { return Redirect::route('named'); });
Create a route that displays a link to the named route:
Route::get('link', function() { return '<a href="' . URL::route('named') . '">Link!</a>'; });
In the browser, visit
http://your-server/redirect
andhttp://your-server/link
(whereyour-server
is the URL for the...