我有3个用户,“ admin”,“ mahasiswa”和“ dosen”(不同的表)。我想,例如,如果用户“ admin”已登录访问主链接(例如:localhost),然后重定向到localhost / admin / home。现在,当用户登录并访问主页(localhost)时,将再次显示登录表单。谢谢
此RedirectIfAuthenticated:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
这是Auth.PHP
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'mahasiswa' => [
'driver' => 'session',
'provider' => 'mahasiswa',
],
'dosen' => [
'driver' => 'session',
'provider' => 'dosen',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'mahasiswa' => [
'driver' => 'eloquent',
'model' => App\Mahasiswa::class,
],
'dosen' => [
'driver' => 'eloquent',
'model' => App\Dosen::class,
],
],
答案 0 :(得分:0)
您可以根据中间件$guard
在中间件中进行检查,可以执行以下操作:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
if($guard == 'mahasiswa')
return redirect('/mahasiswa/home');
elseif($guard == 'dosen')
return redirect('/dosen/home');
elseif($guard == 'web')
return redirect('/admin/home');
else
return redirect('/home');
}
return $next($request);
}