我为登录自定义类型的用户创建了一个新的守卫。我可以使用 登录用户Auth::attempt('webguest')
,但登录不会在页面重新加载后持续存在。它仅在我使用web
-guard 时有效。例如,请参阅我的两条测试路线:
Route::get('test1', function () {
\Auth::guard('webguest')->attempt([
'id' => 5,
'password' => 2222
]);
// here dump(\Auth::guard('webguest')->check()) shows true
return \Redirect::to('/test2');
});
Route::get('test2', function () {
return \Auth::guard('webguest')->check() ? "guest account logged in" : "guest not logged in";
// dumps not logged in
});
我的定制后卫:
config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'webguest' => [
'driver' => 'session',
'provider' => 'guests'
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'guests' => [
'driver' => 'eloquent',
'model' => App\Models\Guest::class
],
]
Guest 和 User 模型都扩展了Illuminate\Foundation\Auth\User as Authenticatable
.
我怎样才能让自定义守卫的登录像守卫一样持续存在web
?