[英]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.