沃梦达 / 编程问答 / php问题 / 正文

Laravel 5.4 中两种不同的身份验证模型

Two different models for authentication in laravel 5.4(Laravel 5.4 中两种不同的身份验证模型)

本文介绍了Laravel 5.4 中两种不同的身份验证模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个不同的模型和表,分别名为 usercompany.

Suppose I have two different models and tables named user and company.

众所周知,laravel 使用 User 模型来管理身份验证.但因为我有两个不同的模型,我想要可以分别管理它们.

As you know laravel uses User model to manage Authentication. but beacause I have two different model I want can manage them separately.

我正在使用 Laravel 5.4,但我不知道如何做到这一点.

I'm using laravel 5.4 and I do not know how can do that.

推荐答案

如果您在谈论多重身份验证系统,那么您必须创建多个保护来实现这一点.

If you are talking about multiple authentication system, then you have to create multiple guards to achieve that.

同一个问题有很好的答案.

There is nice answer to the same question.

谁能用例子解释 Laravel 5.2 Multi Auth

它在 Laravel 5.2 上,但可以在 Laravel 5.4 上轻松实现.

It's on Laravel 5.2, but it can be easily implemented on Laravel 5.4.

  1. 创建一个模型 AppCompany 来扩展可验证类.此模型将用作用户模型,它将公司保护(在下一步中)

  1. Create a model AppCompany which extends Authenticatable Class. This model will work as user model which will company guard (in the next step)

namespace App;

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;

class Company extends Authenticatable
{

    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

}

  • 为模型 AppCompany 创建守卫和提供者.

    // Authenticating guards and providers
    
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
        'company' => [
            'driver' => 'session',
            'provider' => 'company',
        ],
    ],
    
    // Providers 
    
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => AppUser::class,
        ],
        'company' => [
            'driver' => 'eloquent',
            'model' => AppCompany::class,
        ]
    ],
    

  • 现在你可以根据不同的守卫找到用户了.

    Now you can find user according to the different guards.

    $user = Auth::guard('company')->user();
    // Or...
    $user = auth()->guard('company')->user();
    dd($user);
    

    1. 现在为公司 AppHttpControllersAuthCompanyLoginController 创建 Auth 控制器,与 AuthLoginController 相同.指定 $redirectToguard

    1. Now create Auth controller for Company AppHttpControllersAuthCompanyLoginController same as AuthLoginController. Specify $redirectTo and guard

    //AuthComapnyLoginController.php
    
    protected $redirectTo = '/comapany';
    protected $guard = 'comapany';
    
    public function showLoginForm()
    {
        if (view()->exists('auth.authenticate')) {
            return view('auth.authenticate');
        }
    
        return view('comapany.auth.login');
    }
    

    现在为用户创建登录表单 - company.auth.login 视图与用户的登录表单相同.

    now create login form for user - company.auth.login view same as user's login form.

    1. 现在创建路由

    1. Now create routes

    //Login Routes...
    Route::group(['prefix'=>'company', 'middleware'=>'company'], function(){
        Route::get('/login','AuthCompanyLoginController@showLoginForm');
        Route::post('/login','AuthCompanyLoginController@login');
        // ...
        // rest of the company dashboard and other links
        // ...
        Route::get('/logout','AuthCompanyLoginController@logout');
    });
    

  • 为公司创建一个中间件

  • Create a middleware for company

    class RedirectIfNotCompany
    {
        /**
         * Handle an incoming request.
         *
         * @param  IlluminateHttpRequest  $request
         * @param  Closure  $next
         * @param  string|null  $guard
         * @return mixed
         */
        public function handle($request, Closure $next, $guard = 'company')
        {
            if (!Auth::guard($guard)->check()) {
                return redirect('/');
            }
    
            return $next($request);
        }
    }
    

    并将其注册到 kernal.php

    and register it to kernal.php

    protected $routeMiddleware = [
        'company' => AppHttpMiddlewareRedirectIfNotCompany::class,
    ];
    

  • 这就是你所需要的.以警卫的名义访问用户

    And thats all you need. Access user by the name of guard

    Auth::guard('company')->user()
    

    这篇关于Laravel 5.4 中两种不同的身份验证模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:Laravel 5.4 中两种不同的身份验证模型

    基础教程推荐