你好,我是laravel的新手,我正在laravel 5.7上工作,我有2个不同的身份验证表,一个是用户,另一个是公司。首先是我如何检查用户是从哪个表登录的,第二个是我关闭和打开时如果会话存在,浏览器laravel会自动重定向到主页,但我还想添加检查其重定向到特定页面上的用户,这意味着如果用户从用户表登录到abc视图,而公司从公司表登录又重定向到xyz视图我优先?
答案 0 :(得分:0)
这是我为多个身份验证表做的事情:
添加警卫
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
'hash' => false,
],
'company' => [
'driver' => 'session',
'provider' => 'companies',
'hash' => false,
],
],
添加提供商
'providers' => [
'admins' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'companies' => [
'driver' => 'eloquent',
'model' => App\Companies::class,
],
],
添加密码
'passwords' => [
'admins' => [
'provider' => 'admins',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'companies' => [
'provider' => 'companies',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
更新处理方法
public function handle($request, Closure $next, $guard = null)
{
if ($guard === 'admin' && Auth::guard($guard)->check()) {
return redirect('/admin');
}
if ($guard === 'company' && Auth::guard($guard)->check()) {
return redirect('/company');
}
return $next($request);
}
现在我假设您为用户和公司使用了不同的控制器,在这种情况下,您可以在控制器中添加中间件
public function __construct()
{
// For Admin Users
$this->middleware('auth:admin');
// For Companies Users
//$this->middleware('auth:company');
}
或者您也可以在路由中添加中间件
示例
Route::group([
'namespace' => 'Companies',
'middleware' => 'auth:company'
], function () {
// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
});
我希望这会有所帮助。