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

标签 laravel api guard

我有一个应用程序,我在其中创建了一个名为 residents 的新守卫,它是 laravel 附带的默认 User 类的精确副本。但是当我更换防护装置时,它开始显示此错误:
传递给 Illuminate\Auth\SessionGuard::__construct() 的参数 2 必须是 Illuminate\Contracts\Auth\UserProvider 的实例,给定 null,在 ...\vendor\laravel\framework\src\Illuminate\Auth 中调用\AuthManager.php 第 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,
    ],
],

App\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
            ];
        }
    }
}

路由\api.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
        ]);
    }
}

最佳答案

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

  1. 添加用户提供商
  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,
    ],
],

关于Laravel:参数 2 传递给 Illuminate\Auth\SessionGuard::__construct(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57888578/

相关文章:

Ruby Guard 问题 - 'Please install the sqlite3 adapter' - railstutorial.org

php - Laravel artisan 迁移失败

laravel - 如何在 laravel 5 中获取 url 段数?

python - 需要创建一个集合列表,从其成员可能连接的集合列表

ruby-on-rails - LiveReload 不工作 Guard (Firefox)

ruby-on-rails - 如果我使用 Guard 和 Spork,如何在规范或功能中进行 ruby​​ 调试?

php - Laravel,获取 BroadcastOn() 的用户 ID

javascript - 在 Laravel 中使用 ajax FormData() 时无法获取输入字段的值

api - 在 Spotify api 中检测用户的当前歌曲

javascript - 获取多边形中每个点的纬度和经度 - Google Maps API v3