Laravel-5.5里的自带的认证系统中, 路由 Auth::routes()
为什么是这样调用的?
而不是Routes::Auth()
?
文件1: routes\web.php
<?php
Auth::routes();
文件2: vendor\laravel\framework\src\Illuminate\Routing\Router.php
/**
* Register the typical authentication routes for an application.
*
* @return void
*/
public function auth()
{
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');
}
先看源码,看看 Auth::routes( ) 其实是做了什么 ?
底层其实还是手动创建了一个Router实例,并调用了它的auth方法,你所说的 Routes::Auth()也是这样的。
所以,无论你使用 Routes::Auth() 或者 Auth::routes() 其实都是生成 Router 实例并调用了它的 auth方法,作者为什么建议使用 Auth::routes() ,稍微动脑就能想到,Auth::routes()这种写法更容易让人知道生成的路由是和认证权限有关的,和 Auth::id() , Auth::check 等对应,更规范整齐。