繁体   English   中英

如何使用Laravel 5登录用户

[英]How to login user with Laravel 5

任何人都可以帮助我使用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中调用,并且定义的

您不能仅实例化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);

如果你得到

特性'Illuminate \\ Auth \\ UserTrait'

在我看来,这听起来很像Laravel 4(Laravel 5不再具有此特征),是否可能正在迁移应用程序? 看看github上新的默认User模型

使用Auth Facade:

Auth::loginUsingId(1);

相关的Laravel文档文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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