如何在laravel 5.4中使用条件登录后重定向, 如同:
if(Auth::user()->id==1)
{return redirect('admin');}
else
return redirect('/');
如何实施条件。
答案 0 :(得分:19)
在 LoginController
中添加以下代码namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
protected function authenticated(Request $request, $user)
{
if ( $user->isAdmin() ) {// do your margic here
return redirect()->route('dashboard');
}
return redirect('/home');
}
/**
* Where to redirect users after login.
*
* @var string
*/
//protected $redirectTo = '/admin';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
答案 1 :(得分:0)
这是您应该在RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/dashboard'); // Change this line
}
return $next($request);
}
答案 2 :(得分:0)
@ Al-Amin根据角色ID成功登录后重定向您可以在Auth Controller或Login Controller中覆盖经过验证的(Request $ request,$ user)功能,如果您有以下方式:
/**
* [authenticated is used to redirect user after login based on role]
* @param Request $request [request object]
* @param [type] $user [user object to check user role]
* @return [type] [return type]
*/
protected function authenticated(Request $request, $user)
{
if($user->role->id == 1) {
return redirect()->intended('/admin'); // it will be according to your routes.
} else {
return redirect()->intended('/'); // it also be according to your need and routes
}
}