简体   繁体   中英

what is the difference between Auth::routes() and Route::auth() in laravel

I have start working on laravel 5.4 , i have found that there is Auth::routes() which is called default in web route . I want to more clear with the difference between Auth::routes() and Route::auth() .

Using Auth::routes() or Route::auth() is equivalent. Infact Auth::routes() is defined as:

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

where $app->make('router') returns an Illuminate\\Routing\\Router instance just like the facade Route does.

Route::auth() will create the following routes (as found in 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() will call the following function (as found in Illuminate\\Support\\Facades\\Auth.php ):

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

As you can see, the second method creates an instance of the first Router class and calls the auth() function on it. In the end there is no difference in the two methods. If you have a choice I would personally advice using Route::auth() since that seems to be the "default" implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM