我通过登录检查中间件,用户有多少游戏,如果用户有多个游戏,我想显示另一个视图,但如果用户有多个游戏,我每次都会出现重定向错误:
这是$assigned_games > 1
$games = Auth::user()->userGames;
$assigned_games = count($games);
if ($assigned_games == 1) {
return $next($request);
} elseif ($assigned_games > 1) {
return redirect()->route('games.board');
} elseif ($assigned_games < 1) {
echo "no game bought";
exit;
}
我也只尝试使用return route('games.board')
,但它不起作用。
如何在此处设置正确的重定向?
答案 0 :(得分:2)
如果您的路由都在中间件内,那么您应该检查当前路由,如果与重定向相同,则不应重定向以避免重定向循环
$games = Auth::user()->userGames;
$assigned_games = count($games);
if ($assigned_games == 1) {
return $next($request);
} elseif ($assigned_games > 1 && \Route::currentRouteName() != 'games.board') {
return redirect()->route('games.board');
} elseif ($assigned_games < 1) {
echo "no game bought";
exit;
} else {
return $next($request);
}