How to create named route in Laravel


Another cool feature on laravel is route alias or naming route, using named route you can specify a name for your route, so it is easier for you to maintain the routes, i guess.

Here's the typical route that you probably have:
Route::get('/home/login', [ 'uses' => 'LoginController@doLogin' ]);

To add alias/name to that route, simply add another array 'as' => 'route name' like this:
Route::get('/home/login', [ 'uses' => 'LoginController@doLogin', 'as' => 'home.login' ]);

Now you have a route named 'home.login', but you probably wondering what's the benefit of doing this? well, let's say the route for url /home/login is changed, your application will adjust the new url automatically because you are using alias on your code and the alias is still the same.

So every time you change the url structure of the application, the code is working fine, because you use alias on your code. So the question is, how to use the route alias on our laravel code?

Using route name/route alias in the code:
To call for route alias/route name, we can use laravel's helper class 'Route', call the static method 'getCurrentRoute', here's an example:
Route::getCurrentRoute()->getName()
Example code on laravel blade view:
@if (Route::getCurrentRoute()->getName() === 'home.login')
      @include('login.form')
@endif


EmoticonEmoticon