Laravel的auth身份驗證只支持一張表,如果項目中有多個角色存儲在不同表中,比如users admins,就有些不足了,所以需要進行擴展,學的不深,我也無能爲力,但是國外網友有解決方案,並共享在github上。
地址:https://github.com/ollieread/multiauth
按這名老外的說法,這個解決方案並不會替換Laravel本身的auth庫,只是介於auth庫和你的代碼之間的一個工廠類,廢話不多說,看下怎麼安裝使用
安裝
1.先備份的你的代碼
這個不多說,如果你不想在出錯後悲劇的話。
2.打開根目錄下的composer.json,加入你要安裝的包:
"require": {
"ollieread/multiauth": "dev-master"
}
3.更新composer
4.打開app/config/app.php 修改 AuthServiceProvider的配置 Illuminate\Auth\AuthServiceProvider 改成 Ollieread\Multiauth\MultiauthServiceProvider 使用 1.修改app/config/auth.php
return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
替換成
return array(
'multi' => array(
'account' => array(
'driver' => 'eloquent',
'model' => 'Account'
),
'user' => array(
'driver' => 'database',
'table' => 'users'
)
),
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
2.
Auth::account()->attempt(array(
'email' => $attributes['email'],
'password' => $attributes['password'],
));
Auth::user()->attempt(array(
'email' => $attributes['email'],
'password' => $attributes['password'],
));
Auth::account()->check();
Auth::user()->check();