繁体   English   中英

Laravel使用自定义UserServiceProvider中的口才模型

[英]Laravel Use Eloquent model from custom UserServiceProvider

我是Laravel的新手,只是在玩弄它,然后重新投入MVC。

我正在尝试将自己的用户身份验证提供程序(自定义密码哈希)作为一种在Laravel中实现UserProviderInterface的服务。

内部app / controllers / Account.php:

public function postCreate() {
    Auth::attempt(Input::all());
}

我的应用程序通过我的自定义提供程序类路由Auth :: attempt,然后将Input :: all从表单传递给我的方法是resolveByCredentials方法。

内部app / services / PasswordHash / PasswordHashUserProvider.php:

public function retrieveByCredentials(array $credentials) {
    // Why can't I do this?
    //Error: PasswordHash/User not found
    User::find($credentials['username']);

    dd($credentials);
}

这时,我迷失了如何从此服务类访问用户雄辩的模型。 我尝试了命名空间,但没有运气。

服务提供者上的引导方法使用服务容器注入依赖项。 为此,您应该能够执行以下操作(不使用Facades,但是我不经常使用Facades)。

class PasswordHashUserProvider extends ServiceProvider
{
    protected $user;

    public function boot(User $user)
    {
        $this->user = $user;
    }

}

然后,您可以通过$this->user访问$this->user

资料来源: https : //laravel.com/docs/master/providers#the-boot-method

暂无
暂无

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

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