Laravel:参数 2 传递给 Illuminate\Auth\SessionGuard::__construct()

问题描述 投票:0回答:2

我有一个应用程序,我在其中创建了一个名为

residents
的新守卫,它是 laravel 附带的默认
User
类的精确副本。但是当我更换防护罩时,它开始显示此错误:
Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given, called in ...\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on line 125

我尝试在谷歌上搜索解决方案并阅读了很多内容,但没有任何效果。

Laravel 版本是 5.8

auth.php

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'residents',
        'hash' => false,
    ],
],

'providers' => [
    'residents' => [
        'driver' => 'eloquent',
        'model' => App\ApiModels\Resident::class,
    ],
],

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

应用\ApiModels\Resident.php

namespace App\ApiModels;

use \Illuminate\Notifications\Notifiable;
use \Illuminate\Contracts\Auth\MustVerifyEmail;
use \Illuminate\Foundation\Auth\User as Authenticatable;

class Resident extends Authenticatable {
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * Authenticate the user
     *
     * @param   object  $request 
     * @return  array
     */
    public function _login($request) {
        if(\Auth::attempt([
            'email'    => $request->email,
            'password' => $request->password
        ])) {
            return [
                'success' => true
            ];
        } else {
            return [
                'success' => false
            ];
        }
    }
}

路由pi.php

Route::get('/login', 'Api\ResidentsController@login');

Api\ResidentsController.php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * Models
 */
use App\ApiModels\Resident;

class ResidentsController extends Controller {
    public function __construct() {
        /**
         * Middleware(s)
         */
        $this->middleware('auth:api', [
            'except' => [
                'login',
                'logout'
            ]
        ]);
    }

    public function login(Request $request) {
        ...

        return response()->json([
            'login_id' => $request->login_id,
            'password' => $request->password
        ]);
    }
}
laravel api guard
2个回答
31
投票

问题出现在您的

config/auth.php
。您的
web
守卫正在使用
users
提供程序,该提供程序未在您的
providers
数组中声明。您有 2 个选项可以解决此问题(选择一个):

  1. 添加
    users
    提供商
  2. 如果您打算不使用
    web
    防护装置,请将其删除,然后将默认防护装置设置为
    api
'defaults' => [
    'guard' => 'api',
    'passwords' => 'residents',
],

'guards' => [
    'api' => [
        'driver' => 'token',
        'provider' => 'residents',
        'hash' => false,
    ],
],

'providers' => [
    'residents' => [
        'driver' => 'eloquent',
        'model' => App\ApiModels\Resident::class,
    ],
],

'passwords' => [
    'residents' => [
        'provider' => 'residents',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

0
投票

您必须将其用于网络防护:

'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

或者

您可以删除网络防护:

© www.soinside.com 2019 - 2024. All rights reserved.