false )); /* * default response headers */ $app->response->headers->set('Content-Type', 'application/json; charset=utf-8'); // prevent Internet Explorer from caching AJAX requests $app->expires('-1'); /* * api endpoints */ $app->get('/polls/:id', function ($pollId) use ($app) { $poll = Poll::restore($pollId); if (!$poll) { $app->halt(404); } $app->response->setBody( json_encode( array( 'poll' => $poll->export() ) ) ); }); $app->post('/polls', function() use ($app) { $poll = Poll::create( json_decode( $app->request->getBody() )->poll ); $poll->save(); $app->response->setBody( json_encode( array( 'poll' => $poll->export() ) ) ); }); $app->post('/users', function() use ($app) { $user = User::create( json_decode( $app->request->getBody() )->user ); $user->save(); $app->response->setBody( json_encode( array( 'user' => $user->export() ) ) ); }); /* * error handling */ $app->error(function() use ($app) { $app->halt(500); }); $app->notFound(function() use ($app) { $app->halt(404); }); $app->run();