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

Posted

技术标签:

【中文标题】Laravel:参数 2 传递给 Illuminate\\Auth\\SessionGuard::__construct()【英文标题】: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
        ]);
    

【问题讨论】:

【参考方案1】:

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

    添加users 提供者 如果您不打算使用 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()的主要内容,如果未能解决你的问题,请参考以下文章

从 laravel 5 升级到 laravel 5.5 导致错误参数 1 传递给 App\Exceptions\Handler::report()

Laravel 5.8:传递给事件侦听器的参数变为空

如何将 null 参数从播种机传递给 Laravel 工厂?

传递给验证器()的 Laravel 参数 1 必须是 Illuminate\Http\Request 的实例,给定数组

传递给 Illuminate\Database\Grammar::parameterize() 的 Laravel 工厂参数 1 必须是数组类型,给定字符串

传递给方法的 Laravel 错误参数 1 必须是 App\Model 的实例,给定的 Illuminate\Database\Eloquent\Collection 的实例