Laravel Routing Named Route

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Named routes are used to generate a URL or redirects to a specific route. The advantage of using a named route is, if we change the URI of a route in future, we wouldn't need to change the URL or redirects pointing to that route if we are using a named route. But if the links were generated using the url [ eg. url('/admin/login')], then we would have to change everywhere where it is used.

Named routes are created using as array key

Route::get('login', ['as' => 'loginPage', 'uses' => 'LoginController@index']);

or using method name

Route::get('login', 'LoginController@index')->name('loginPage');

Generate URL using named route

To generate a url using the route's name

$url = route('loginPage');

If you are using the route name for redirection

$redirect = Redirect::route('loginPage');


Got any Laravel Question?