ujw*_*kal 6 php service-provider laravel laravel-5.2
我为我的laravel 5.2进行自定义身份验证时收到错误但是此代码在我的laravel 5.1 My config/auth.php文件中正常工作
'providers' => [
'users' => [
'driver' => 'custom',
'model' => App\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
Run Code Online (Sandbox Code Playgroud)
我的CustomUserProvider.php(Auth/CustomUserProvider)文件
<?php namespace App\Auth;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
class CustomUserProvider implements UserProvider {
protected $model;
public function __construct(UserContract $model)
{
$this->model = $model;
}
public function retrieveById($identifier)
{
}
public function retrieveByToken($identifier, $token)
{
}
public function updateRememberToken(UserContract $user, $token)
{
}
public function retrieveByCredentials(array $credentials)
{
}
public function validateCredentials(UserContract $user, array $credentials)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我的CustomAuthProvider.php文件
<?php namespace App\Providers;
use App\User;
use Auth;
use App\Auth\CustomUserProvider;
use Illuminate\Support\ServiceProvider;
class CustomAuthProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->app['auth']->extend('custom',function()
{
return new CustomUserProvider();
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
Run Code Online (Sandbox Code Playgroud)
现在这在laravel 5.1 in 5.2中工作正常我得到的错误就像
InvalidArgumentException in CreatesUserProviders.php line 40:
Authentication user provider [custom] is not defined.
Run Code Online (Sandbox Code Playgroud)
唯一的一点是使用
$this->app['auth']->provider(...
Run Code Online (Sandbox Code Playgroud)
代替
$this->app['auth']->extend(...
Run Code Online (Sandbox Code Playgroud)
最后一个在5.1中使用,第一个应该在5.2中使用.
尝试替换引导函数,如下所示:
public function boot()
{
Auth::provider('custom', function($app, array $config) {
// Return an instance of Illuminate\Contracts\Auth\UserProvider...
return new CustomUserProvider($app['custom.connection']);
});
}
Run Code Online (Sandbox Code Playgroud)