创建中间件后,我在Laravel应用中获得了多个重定向,这肯定是我做错了,但是 我不知道在哪里。
public function handle($request, Closure $next)
{
$route = Route::currentRouteName();
if($route!= "dashboard" || $route != "verify.again")
{
if(! is_null($request->user()) && ! $request->user()->verified) {
return redirect()->route("verify.again");
}
elseif(!$request->user()->mobile){
return redirect()->route("verify.again");
}
else{
return $next($request);
}
}
return $next($request);
}
控制器
public function index(){
$user = Auth::user();
if(!$user->verified)
{
return view("dashboard.verification.index", ["verify"=>"email", ]);
}
elseif(!$user->mobile)
{
return view("dashboard.verification.index", ["verify"=>"mobile", ]);
}
return view("dashboard.usermaster");
}
答案 0 :(得分:0)
对于if
路由,您的true
语句将始终为verify.again
,因为"verify.again"
始终与"dashboard"
不同。这导致重定向到相同路由和中间件的无限循环。因此,解决方案实际上可能是将AND ||
替换为OR &&
:
if($route != "dashboard" && $route != "verify.again") {
// your code here...
}