問題描述
假設我有兩個不同的模型和表,分別名為 user
和 company
.
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.
創建一個模型 AppCompany 來擴展可驗證類.此模型將用作用戶模型,它將公司保護(在下一步中)
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);
現在為公司 AppHttpControllersAuthCompanyLoginController 創建 Auth 控制器,與 AuthLoginController 相同.指定 $redirectTo 和 guard
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.
現在創建路由
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 中兩種不同的身份驗證模型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!