我在laravel中为用户制作新的自定义模型。我为一种类型的用户使用默认的User
laravel模型,为其他类型的用户使用新的Merchant
模型。
我在register
视图中选择选项以选择将注册哪种类型的用户以便在控制器中实现更好的控制。
<select id="user_type" name="user_type">
<option value="user">User</option>
<option value="merchant">Merchant</option>
</select>
这是我修改了两种类型用户的默认RegisterController
:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Merchant;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/index';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
public function register(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
//Add custom code here
$new_user = $this->create($request->all());
//Add custom code here
Auth::guard($this->getGuard())->login($new_user);
return redirect($this->redirectPath());
}
protected function validator(array $data)
{
if($data['user_type']=='user'){
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'user_type' =>'required'
]);
}else{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'user_type' =>'required'
]);
}
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
if($data['user_type']=='user'){
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
]);
}else{
return Merchant::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
]);
}
}
}
在register
中默认不提及register controller
功能,所以我放了这个功能,因为我已经查看了解决方案How to register more than one type of users and how to make multi auth in laravel 5.3,但我不知道如何修改这一点。
但现在的问题是我提交注册表后的身份验证。如果我注册了新的user
它正常工作,但是如果我注册merchant
它会将用户保存在数据库中,之后如果没有&#39则它就不会登录商家; t在user
表中存在任何users
。但是,如果在注册user
之后users
中已存在任何merchant
,则会在user
表中创建的最后users
中登录。
所以我的问题是如何修改商家用户的身份验证。
谢谢!
更新
注册功能
public function register(Request $request)
{
if($request->user_type =='user'){
$auth = auth()->guard();
} else{
$auth = auth()->guard('merchant');
}
$user = $this->create($request->all());
auth()->login($user);
return redirect($this->redirectPath());
}
答案 0 :(得分:8)
/config/auth.php
查找数组的guards
元素
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
为merchant
值 provider
merchants
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'merchant' => [
'driver' => 'session',
'provider' => 'merchants',
],
],
现在找到此配置的providers
元素
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
添加merchants
提供商,其model
模型的值为Merchant
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'merchants' => [
'driver' => 'eloquent',
'model' => App\Merchant::class,
],
],
现在,您只需通过guard('web')
guard('merchant')
和商家用户对普通用户进行身份验证
醇>
并为您注册功能
public function register(Request $request)
{
$guard = $request->user_type == 'user' ? 'web' : 'merchant';
$user = $this->create($request->all());
auth()->guard($guard)->login($user);
return redirect($this->redirectPath());
}
您也可以使用套餐......
例如,有一个很棒的包:https://github.com/Sarav-S/Laravel-Multiauth
它将帮助您处理所有困难的事情。
答案 1 :(得分:1)
首先您需要在guard
设置新的auth.php
。当使用merchant
登录时更改防护方法,以便Laravel会调查商家而不是用户。
if($request->user_type =='user'){
$auth = auth()->guard();
} else{
$auth = auth()->guard('merchant');
}
$auth()->login($user);
return redirect($this->redirectPath());