Laravel 5.4 EloquentUserProvider 覆盖 validateCredentials
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44069557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4 EloquentUserProvider override validateCredentials
提问by Sergey Somok
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
How to override a method validateCredentialsin the class EloquentUserProvider?? Thanks!
如何覆盖validateCredentials类中的方法EloquentUserProvider?谢谢!
采纳答案by Troyer
You can create your own UserProvider and then you can override the functions from the original UserProvider.
您可以创建自己的 UserProvider,然后您可以覆盖原始 UserProvider 的功能。
First you create the CustomUserProvider:
首先创建 CustomUserProvider:
use Illuminate\Contracts\Auth\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());
}
}
Then you register your new CustomUserProvider in config/app.php
然后在 config/app.php 中注册新的 CustomUserProvider
'providers' => array(
... On the bottom, must be down to override the default UserProvider
'Your\Namespace\CustomUserProvider'
),
回答by mhellmeier
In Laravel 5.4 you don't need to register your CustomUserProviderin config/app.php.
在 Laravel 5.4 中,您不需要在config/app.php 中注册您的CustomUserProvider。
See this blog articlefor detailed instructions.
有关详细说明,请参阅此博客文章。
To short form:
简写:
First, create a CustomUserProvider.phpfile in your Providersdirectory:
首先,在您的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());
}
}
After this, change the boot()Method in your AuthServiceProvider.phpfile:
在此之后,更改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']);
});
}
Now, you can use the provider by adding the driver name to your config/auth.phpfile:
现在,您可以通过将驱动程序名称添加到您的config/auth.php文件来使用提供程序:
'providers' => [
'users' => [
'driver' => 'customuserprovider',
'model' => App\User::class,
'table' => 'users',
],
],

