2017-05-19 18 views

回答

0

您可以創建自己的UserProvider,然後可以覆蓋原始UserProvider中的功能。

首先創建CustomUserProvider:

use Illuminate\Contracts\Auth\UserProvider; 

class CustomUserProvider extends UserProvider { 

    public function validateCredentials(UserContract $user, array $credentials) 
    { 
    $plain = $credentials['password']; 

    return $this->hasher->check($plain, $user->getAuthPassword()); 
    } 

} 

然後您註冊新CustomUserProvider在配置/ app.php

'providers' => array(
    ... On the bottom, must be down to override the default UserProvider 
    'Your\Namespace\CustomUserProvider' 
), 
8

在Laravel 5.4,你不需要註冊CustomUserProviderconfig/app.php

首先,在你提供商創建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/a中來使用該提供程序uth.php file:

'providers' => [ 
    'users' => [ 
     'driver' => 'customuserprovider', 
     'model' => App\User::class, 
     'table' => 'users', 
    ], 
],