Laravel 5:Auth :: guard($ this-> getGuard()) - > login($ user)之间的区别;和auth() - > login($ user);

时间:2016-02-25 10:24:37

标签: php laravel model-view-controller laravel-5 laravel-5.1

有什么区别:

Auth::guard($this->getGuard())->login($user);

auth()->login($user);

?例如,在PasswordController.php中我们可以:

protected function resetPassword($user, $password)
{
    $user->password = $password;

    $user->save();

    Auth::guard($this->getGuard())->login($user);
}

protected function resetPassword($user, $password)
{
    $user->password = $password;

    $user->save();

    auth()->login($user);
}

(在这种情况下,我们在 Users.php 中创建一个mutator来加密密码而不是 resetPassword($ user,$ password),因为它是默认的)

特别是什么是后卫($ this-> getGuard()),它做了什么(guard(),getGuard())?

1 个答案:

答案 0 :(得分:3)

没有区别,auth函数只是一个返回\Illuminate\Contracts\Auth\Guard实例的帮助器。

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Foundation/helpers.php