laravel中Auth :: routes()和Route :: auth()之间的区别是什么

时间:2017-09-28 07:17:14

标签: php laravel authentication laravel-5

我已经开始处理 laravel 5.4 ,我发现在网络路径中有Auth::routes()被称为默认值。我希望更清楚Auth::routes()Route::auth()之间的区别。

2 个答案:

答案 0 :(得分:4)

使用Auth::routes()Route::auth()是等效的。实际上Auth::routes()定义为:

/**
 * Register the typical authentication routes for an application.
 *
 * @return void
 */
public static function routes()
{
    static::$app->make('router')->auth();
} 

其中$app->make('router')返回Illuminate\Routing\Router实例,就像外观Route一样。

答案 1 :(得分:1)

Route::auth()将创建以下路线(如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()将调用以下函数(如Illuminate\Support\Facades\Auth.php中所示):

/**
 * Register the typical authentication routes for an application.
 *
 * @return void
 */
public static function routes()
{
    static::$app->make('router')->auth();
}

如您所见,第二种方法创建第一个Router类的实例,并在其上调用auth()函数。最后两种方法没有区别。如果您有选择,我个人会建议使用Route::auth(),因为这似乎是"默认"实施