作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为laravel中的用户制作新的自定义模型。我正在为将要使用的一种类型的用户使用默认的User
laravel模型,并为其他类型的用户使用新的Merchant
模型。
我在register
View 中进行选择,以选择要注册哪种类型的用户,以便在 Controller 中进行更好的控制。
<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
,它将用户保存在数据库中,此后如果
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());
}
最佳答案
/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());
}
关于laravel - 如何在Laravel中对自定义模型使用身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40993143/
我是一名优秀的程序员,十分优秀!