Laravel 7 多重身份验证使用 Laravel UI
Laravel 7 Muti Auth using Laravel UI
我正在为我叔叔的商店开发一个小型食品网站。网上商店可以有不同类型的用户:管理员、客户和送货员。
每个用户在数据库中都有不同的表。我也相应地更新了 config/auth.php 但仍然不起作用。当我尝试注册客户时出现错误:
Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must implement interface Illuminate\Contracts\Auth\UserProvider, null given, called in /var/www/html/pizzaShop/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 125
config/auth.php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'customers',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'customer' => [
'driver' => 'session',
'provider' => 'customers',
],
'delivery_boy' => [
'driver' => 'session',
'provider' => 'delivery_boys',
],
'api' => [
'driver' => 'token',
'provider' => 'customers',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'customer' => [
'driver' => 'eloquent',
'model' => App\Customer::class,
],
'delivery_boy' => [
'driver' => 'eloquent',
'model' => App\DeliveryBoy::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
在COntroller/Auth下注册控制器
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest');
$this->middleware('guest:admin');
$this->middleware('guest:customer');
$this->middleware('guest:delivery_boy');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
public function viewRegisterForm() {
return View('frontend.customer.auth-view', ['authType'=>'register']);
}
public function registerUser(Request $request) {
dd($request);
return View('frontend.customer.auth-view', ['authType'=>'register']);
}
}
我的寄存器的网络路由
Route::get('/auth/', function () {
return view('frontend.customer.auth-view');
});
Route::group(['prefix' => 'auth'], function () {
Route::get('register/', 'Auth\RegisterController@viewRegisterForm');
Route::post('register/', 'RegisterController@registerUser');
Route::get('login/', 'Auth\LoginController@viewLoginForm');
Route::post('login/', 'LoginController@loginUser');
});
Auth::routes();
我使用的是我自己的视图,而不是由 Laravel UI 创建的,但是我的视图的表单路径是 action="{{ route('login') }}"
和 action="{{ route('register') }}"
。希望这不会成为问题。
Url 通过浏览器导航注册:
http://localhost:8000/auth/register
和错误
我正在使用 laravel 7 和 php7.4。
您没有名为 admins
、customers
或 delivery_boys
的用户提供者,因为某些警卫正在寻找 'provider'
。你用单数命名它们:
'providers' => [
'users' => [...],
'admin' => [...],
'customer' => [...],
'delivery_boy' => [...],
]
我正在为我叔叔的商店开发一个小型食品网站。网上商店可以有不同类型的用户:管理员、客户和送货员。
每个用户在数据库中都有不同的表。我也相应地更新了 config/auth.php 但仍然不起作用。当我尝试注册客户时出现错误:
Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must implement interface Illuminate\Contracts\Auth\UserProvider, null given, called in /var/www/html/pizzaShop/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 125
config/auth.php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'customers',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'customer' => [
'driver' => 'session',
'provider' => 'customers',
],
'delivery_boy' => [
'driver' => 'session',
'provider' => 'delivery_boys',
],
'api' => [
'driver' => 'token',
'provider' => 'customers',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'customer' => [
'driver' => 'eloquent',
'model' => App\Customer::class,
],
'delivery_boy' => [
'driver' => 'eloquent',
'model' => App\DeliveryBoy::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
在COntroller/Auth下注册控制器
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest');
$this->middleware('guest:admin');
$this->middleware('guest:customer');
$this->middleware('guest:delivery_boy');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
public function viewRegisterForm() {
return View('frontend.customer.auth-view', ['authType'=>'register']);
}
public function registerUser(Request $request) {
dd($request);
return View('frontend.customer.auth-view', ['authType'=>'register']);
}
}
我的寄存器的网络路由
Route::get('/auth/', function () {
return view('frontend.customer.auth-view');
});
Route::group(['prefix' => 'auth'], function () {
Route::get('register/', 'Auth\RegisterController@viewRegisterForm');
Route::post('register/', 'RegisterController@registerUser');
Route::get('login/', 'Auth\LoginController@viewLoginForm');
Route::post('login/', 'LoginController@loginUser');
});
Auth::routes();
我使用的是我自己的视图,而不是由 Laravel UI 创建的,但是我的视图的表单路径是 action="{{ route('login') }}"
和 action="{{ route('register') }}"
。希望这不会成为问题。
Url 通过浏览器导航注册:
http://localhost:8000/auth/register
和错误
我正在使用 laravel 7 和 php7.4。
您没有名为 admins
、customers
或 delivery_boys
的用户提供者,因为某些警卫正在寻找 'provider'
。你用单数命名它们:
'providers' => [
'users' => [...],
'admin' => [...],
'customer' => [...],
'delivery_boy' => [...],
]