首页 文章

如何使用Laravel 5登录用户

提问于
浏览
0

任何人都可以帮我用Laravel登录用户,这是我的尝试:

public function execute($hasCode){
    if(!$hasCode) return $this -> getAuthorizationFrist();  
    $user = $this->socialite->driver('facebook')->user();
    echo $user->getNickname();
    echo $user->getName();
    echo $user->getEmail();
    echo $user->getAvatar();


    $login = new Guard(); 
    $login ->loginUsingId(1);

}

这是错误:

传递给Illuminate \ Auth \ Guard :: __ construct()的参数1必须是Illuminate \ Contracts \ Auth \ UserProvider的实例,没有给出,在第31行的/home/comment/public_html/bild/app/AuthenticateUser.php中调用并定义

2 回答

  • 2

    您不能只是实例化 Guard ,因为它具有在创建时需要注入的依赖项 . 这是构造函数:

    public function __construct(UserProvider $provider,
                                SessionInterface $session,
                                Request $request = null)
    

    你有几个选择:

    1.使用立面:

    Auth::loginUsingId(1);
    

    2.使用IoC容器:

    $auth = app('auth');
    $auth->loginUsingId(1);
    

    3.使用依赖注入(推荐):

    在类的构造函数中,您要使用此:

    public function __construct(\Illuminate\Auth\Guard $guard){
        $this->auth = $guard;
    }
    

    在你的方法中:

    $this->auth->loginUsingId(1);
    

    如果你得到了

    Trait'LIuminate \ Auth \ UserTrait'

    这对我来说听起来很像Laravel 4(Laravel 5不再具有这种特性)你有可能正在迁移你的应用程序吗?看看新的default User model on github

  • 0

    使用Auth Facade:

    Auth::loginUsingId(1);
    

    Relevant laravel docs article

相关问题