简体   繁体   English

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

[英]Laravel Use Eloquent model from custom UserServiceProvider

I'm new to Laravel, just toying with it and getting my head back into MVC. 我是Laravel的新手,只是在玩弄它,然后重新投入MVC。

I'm trying to make my own User auth provider (custom password hashing) as a service that implements the UserProviderInterface within Laravel. 我正在尝试将自己的用户身份验证提供程序(自定义密码哈希)作为一种在Laravel中实现UserProviderInterface的服务。

Inside app/controllers/Account.php: 内部app / controllers / Account.php:

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

I have my app routing Auth::attempt through my custom provider class, and passing me the Input::all from the form into a retrieveByCredentials method. 我的应用程序通过我的自定义提供程序类路由Auth :: attempt,然后将Input :: all从表单传递给我的方法是resolveByCredentials方法。

Inside app/services/PasswordHash/PasswordHashUserProvider.php: 内部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);
}

I am lost at this point on how to access my User eloquent models from within this service class. 这时,我迷失了如何从此服务类访问用户雄辩的模型。 I tried namespaces but had no luck. 我尝试了命名空间,但没有运气。

The boot method on service providers use the service container to inject dependencies. 服务提供者上的引导方法使用服务容器注入依赖项。 To that end, you should be able to do the following (not using Facades, but I don't use facades that often). 为此,您应该能够执行以下操作(不使用Facades,但是我不经常使用Facades)。

class PasswordHashUserProvider extends ServiceProvider
{
    protected $user;

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

}

You can then access user via $this->user 然后,您可以通过$this->user访问$this->user

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

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

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