Named Routes

In a small application where you have defined small number of routes, its ok for use route without naming convention. It is not a big deal but if you working on a big scale projects where 50 or 100's of routes are defined, you should use named routes. Its like nick name of every route and it will very handy for every developers.

Here is simple example of named routes:-

Route::get( 'user/profile', [ 'as'=>'profile', function(){
	return “Named routing with Closure”;
}] );

In the above example we have a route for user/profiles path in URI but we can call this path with name profile :

//with blade template engine.
{{ route('profile') }}

// It will only generate url not a link                        //
// For a link you should use link_to_route or                  //  
//  you can use route like this                                // 
//  <a href="{{route('your route name') }}">go there</a>       //
//                                                             //

//with simple php code.
<?php return Redirect::route('profile'); ?>

//with generating URLs or redirects.
$url = route('profile');
$redirect = Redirect::route('profile');

There are 3 ways to redirect route request. Use however you find easy in your application. In the future If you want to change URL of route then just update route url, code will not break.

Named route with Controller

Route::get( 'user/profile', [ 'as'=>'profile', 'uses'=>'UserController@show'] );

Now we using Controller class which name is UserController and show is method. Inside show method you put your logic and route redirection like we mention above.

Something to say? Tell us in comment section.