getUri()->getPath(); $publicPaths = ['/login', '/setup']; $isPublic = in_array($path, $publicPaths, true); if (!isset($_SESSION['user_id']) && !$isPublic) { $response = new \Slim\Psr7\Response(); return $response ->withHeader('Location', '/login') ->withStatus(302); } // If logged in and trying to access login/setup, redirect to dashboard if (isset($_SESSION['user_id']) && $isPublic) { $response = new \Slim\Psr7\Response(); return $response ->withHeader('Location', '/dashboard') ->withStatus(302); } return $handler->handle($request); } }