Laravel 5: The difference between Auth::guard($this->getGuard())->login($user); and auth()->login($user);

旧巷老猫 提交于 2020-01-14 22:54:04

问题


What is the difference between:

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

and

auth()->login($user);

? For example, in PasswordController.php we can have:

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

    $user->save();

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

or

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

    $user->save();

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

(in this case, we create a mutator in Users.php to bcrypt password AND NOT in resetPassword($user, $password) as it is by default)

In particular, what is guard($this->getGuard()), what does it do (guard(), getGuard())?


回答1:


There is no difference, the auth function is just a helper that returns an instance of \Illuminate\Contracts\Auth\Guard.

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



来源:https://stackoverflow.com/questions/35624561/laravel-5-the-difference-between-authguardthis-getguard-loginuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!