如何在Laravel

时间:2016-12-06 10:31:16

标签: laravel laravel-5

我在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());


    }

2 个答案:

答案 0 :(得分:8)

  1. 转到/config/auth.php
  2. 查找数组的guards元素

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ],
    
  3. merchant provider

    的数组添加一个警卫merchants
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    
        'merchant' => [
            'driver' => 'session',
            'provider' => 'merchants',
        ],
    ],
    
  4. 现在找到此配置的providers元素

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    ],
    
  5. 添加merchants提供商,其model模型的值为Merchant

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    
        'merchants' => [
            'driver' => 'eloquent',
            'model' => App\Merchant::class,
        ],
    ],
    
  6. 现在,您只需通过guard('web')

  7. 通过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());

https://laravel.com/docs/5.3/authentication