web-dev-qa-db-ja.com

Laravel:aumplise 2 \ auth \ sessionguard :: __ construct()に渡されました

Laravelに付属のデフォルトresidentsクラスの正確なコピーである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',
    ];

    /**
     * @Author         : Parminder Singh
     * @Last modified  : 09, August 2019
     *
     * @Function name  : _login
     * @Description    : Function to authenticate the user
     * @Parameters     : $request as $object
     *
     * @Method         :
     * @Returns        : raw data
     * @Return type    : array
     */
    public function _login($request)
    {
        if(\Auth::attempt([
            'email'    => $request->email,
            'password' => $request->password
        ]))
        {
            return [
                'success' => true
            ];
        }
        else
        {
            return [
                'success' => false
            ];
        }
    }
}
 _

route\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
        ]);
    }
}
 _
6
MrSingh

問題はconfig/auth.phpで発生します。 web guardは、users配列で宣言されていないprovidersプロバイダを使用しています。これを修正するための2つのオプションがあります(1つ選択)。

  1. usersプロバイダを追加します
  2. web Guardを使用しない場合は、代わりにデフォルトガードを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,
    ],
],
8
aceraven777