【问题标题】:Laravel: Argument 2 passed to Illuminate\Auth\SessionGuard::__construct()Laravel:参数 2 传递给 Illuminate\Auth\SessionGuard::__construct()
【发布时间】:2020-01-13 06:15:56
【问题描述】:

我有一个应用程序,我在其中创建了一个名为 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,
    ],
],

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
        ]);
    }
}

【问题讨论】:

    标签: laravel api guard


    【解决方案1】:

    问题出现在您的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,
        ],
    ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 2020-01-04
      • 2021-12-22
      • 2020-07-11
      • 2019-09-13
      • 2020-01-04
      • 2019-12-05
      相关资源
      最近更新 更多