Laravel路由后面带个`name('register')`作用是什么?

下面这个路由后面带个name('register')作用是什么?

$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Tomoe

就是給該路由取一個別名,就可以很方便的指定路由,而不用打一串網址,例如

// 路由
Route::get('user/profile', 'UserController@profile')->name('profile');

// 產生對應網址
$url = route('profile');

// 轉址
return redirect()->route('profile');
canihelpyou 回复 Tomoe

可是题目中的路由前面和后面是一样的,这个路由是 laravel5.3 auth自带的。

Tomoe 回复 canihelpyou

那只是剛好一樣而已,你也可以把 name('register') 改成 name('signup') 之類的,兩者不能混為一談,前者 get('register')網址,後者 name('register')別名,而實際使用上也會有差別,延續上面那個例子:

// 根據 name 轉址
redirect()->route('profile'); 

// 根據網址
redirect()->to('user/profile')
imagine10255

請問別名命名的規則 有什麼建議嗎 ?
(如果不看使用習慣來說的話)

Tomoe 回复 imagine10255

因為別名是全局的,所以最好標示清楚,例如

文章列表 post-list
文章 post
新增文章頁面 post-create
儲存文章 post-store
編輯文章 post-edit
更新文章 post-update

最好是屬於一個領域的同一個前輟(prefix),文章就是 post-xxxx,回覆就是 reply-xxx,會員相關就是 user-xxx 之類的,沒有任何硬性規定,怎麼清楚怎麼來。

中間的分隔符也隨意,看過 - _ . : 這幾種

imagine10255 回复 Tomoe

感謝.很好的建議

JellyBool 回复 imagine10255

恩,是的,我也是使用 Tomoe 的方法,只不过我用 .作为分隔符:

post.create
post.store

这样

imagine10255 回复 JellyBool

感謝.很好的建議