// Matches if the HTTP method is GET -> /statuses $app->get('/statuses', function (Request $request) use($app, $statusFinder) { $data = array('status' => $statusFinder->findAll()); if ($request->guessBestFormat() === 'json') { return new JsonResponse($data); } if (isset($_SESSION['userName'])) { array_push($data, 'userName', $_SESSION['userName']); } else { array_push($data, 'userName', "NoUser"); } return $app->render('index.php', $data); }); // Matches if the HTTP method is GET -> /statuses/id $app->get('/statuses/(\\d+)', function (Request $request, $id) use($app, $statusFinder) { if (null === ($status = $statusFinder->findOneById($id))) { throw new HttpException(404); } $data = array('status' => $status); if ($request->guessBestFormat() === 'json') { return new JsonResponse($data); } return $app->render('status.php', $data); }); // Matches if the HTTP method is POST -> /statutes $app->post('/statuses', function (Request $request) use($app, $statusFinder, $statusMapper) { $status = new Status(null, htmlspecialchars($request->getParameter('user')), htmlspecialchars($request->getParameter('message')), date("Y-m-d H:i:s")); $statusMapper->persist($status); if ($request->guessBestFormat() === 'json') { return new JsonResponse("statuses/" . count($statusFinder->findAll()), 201); }
} if (isset($_SESSION['user'])) { $data['user'] = $_SESSION['user']; } else { $data['user'] = '******'; } return $app->render('index.php', $data); }); // Matches if the HTTP method is GET -> /statuses/id $app->get('/statuses/(\\d+)', function (Request $request, $id) use($app, $statusFinder) { if (!Validation::isInt($id)) { $response = new Response("Incorrect id parameter", 400); $response->send(); return; } if (null === ($data['status'] = $statusFinder->findOneById($id))) { if ($request->guessBestFormat() === 'json') { return new JsonResponse(json_encode("Status not found"), 204); } throw new HttpException(404, 'Status not found'); } if ($request->guessBestFormat() === 'json') { return new JsonResponse(json_encode($data['status']), 201); } if (isset($_SESSION['user'])) { $data['user'] = $_SESSION['user']; } else { $data['user'] = '******'; } return $app->render('status.php', $data); });