在 Laravel 5.4 中,您无需在 config/app.php 中注册您的 CustomUserProvider。
有关详细说明,请参阅this blog article。
简称:
首先,在您的 Providers 目录中创建一个 CustomUserProvider.php 文件:
<?php
namespace App\Providers;
use Illuminate\Auth\EloquentUserProvider as UserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
class CustomUserProvider extends UserProvider {
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
}
在此之后,更改 AuthServiceProvider.php 文件中的 boot() 方法:
public function boot()
{
$this->registerPolicies();
\Illuminate\Support\Facades\Auth::provider('customuserprovider', function($app, array $config) {
return new CustomUserProvider($app['hash'], $config['model']);
});
}
现在,您可以通过将驱动程序名称添加到您的 config/auth.php 文件来使用提供程序:
'providers' => [
'users' => [
'driver' => 'customuserprovider',
'model' => App\User::class,
'table' => 'users',
],
],