如何在Laravel 5.3中注册不止一种类型的用户以及如何进行多重身份验证 [英] How to register more than one type of users and how to make multi auth in laravel 5.3

查看:126
本文介绍了如何在Laravel 5.3中注册不止一种类型的用户以及如何进行多重身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有人会注意到我再次问这个问题,但是我有理由.关于此问题,我之前曾问过两个问题,但找不到很好的答案和答案.

Maybe someone will notice that I'm asking this question again but I have reason for that. I have asked two questions before about this problem and I didn't find any good еxplanation and answer..

如何在Laravel中创建两种类型的用户

在Laravel 5.3中注册新类型的用户

我现在将尽力更好地解释我的总体问题.我正在尝试注册两种类型的用户,我的想法是为该用户拥有两个不同的表,第二个问题是我在注册用户on one same login form后如何进行多重身份验证.

I will try to explain better now what is my question in general. I'm trying to register two types of users, and my idea is to have two different tables for that users, and the second problem that I have is how to make multi authentication after I register the user on one same login form.

我一直在寻找答案,但找不到任何好的答案.

I'm searching for answers for long time and I didn't find any good answer.

非常感谢您!

推荐答案

您必须覆盖 Laravel中的内置身份验证功能.

You have to override the built-in authentication function in Laravel.

注意:对于laravel 5.3 :函数位于RegisterController.php中,您必须php artisan make:auth才能创建这些控制器(请参阅

NOTE: For laravel 5.3 : the functions are in RegisterController.php and you have to php artisan make:auth to create these controllers (refer to https://laravel.com/docs/5.3/authentication).

注意:对于laravel 5.2及更低版本: 当您查看AuthController.php时,您会看到它使用了特征AuthenticatesAndRegistersUsers.尝试在Laravel的供应商php文件(... \ Illuminate \ Foundation \ Auth ...)中查看此特征中的所有功能,然后可以通过将所需的更改应用于AuthController.php中来重用(覆盖)它们.他们.

NOTE: For laravel 5.2 and lower : When you look at your AuthController.php, you will see it uses the trait AuthenticatesAndRegistersUsers. Try to look at all the functions in this trait in Laravel's vendor php files (...\Illuminate\Foundation\Auth...) and you can re-use (override) them in your AuthController.php by applying your desired changes to them.

在您的情况下,您可能想更改3个功能registervalidatorcreate.因此,请在身份验证控制器中找到这些功能,然后添加您的自定义代码.

In your case, you probably want to change 3 functions register,validator and create. So find these functions in the authentication controller and add your custom code.

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());
}

//Here you can differentiate the columns for each type of users...
protected function create(array $data)
{
  if ($data['user_type']=='Type1'){
    return UserType1::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'customInfoType1' => 'this is specific to User Type 1'
    ]);
  }else{
    return UserType2::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'customInfoType2' => 'this is specific to User Type 2'
    ]);
  }
}

//And don't forget to change the validator if the validation rules are different for each type
protected function validator(array $data)
{
  if ($data['user_type']=='Type1'){
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users_type1',
        'password' => 'required|min:6',
        'customInfoType1' => 'whatever validation rules'
    ]);
  }else{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users_type2',
        'password' => 'required|min:6',
        'customInfoType2' => 'whatever validation rules'
    ]);
  }
}

这篇关于如何在Laravel 5.3中注册不止一种类型的用户以及如何进行多重身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆