Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Route, ->name method?

Laravel 5.5 which is the different doing (no get and post methods) on route defintion web.php file:

$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');

regarding ->name('') method

Is required define that method? in which cases?

(sample taked from Auth Class definition laravel)

like image 365
user3186511 Avatar asked Dec 14 '22 19:12

user3186511


1 Answers

The idea of defining ->name() in routes is for easier code maintenance in the future, it's not mandatory.

Say for example you have few places which uses the route login, one fine day you update the route to user-login. You will have to find and update all the route being used, changing from url('login') to url('user-login').

If you have a route name defined, you will be using route('login'), when you update your route url, there's no need to update all the other files that you're using that route.

like image 140
SteD Avatar answered Dec 17 '22 22:12

SteD