$config['debug'] )); // add expires header to all responses to // prevent Internet Explorer from caching AJAX requests $app->add(function (Request $request, Response $response, $next) { $response = $response->withHeader('Expires', '-1'); return $next($request, $response); }); /* * api endpoints */ $app->get('/polls/{id}', function (Request $request, Response $response) { $pollId = $request->getAttribute('id'); $poll = Poll::restore($pollId); if (!$poll) { return $response->withStatus(404); } return $response->withJSON( array( 'poll' => $poll->export() ) ); }); $app->post('/polls', function (Request $request, Response $response) { $poll = Poll::create( json_decode( $request->getBody() )->poll ); $poll->save(); return $response->withJSON( array( 'poll' => $poll->export() ) ); }); $app->post('/users', function (Request $request, Response $response) { $user = User::create( json_decode( $request->getBody() )->user ); $user->save(); return $response->withJSON( array( 'user' => $user->export() ) ); }); /* * error handling */ $c = $app->getContainer(); $c['errorHandler'] = function ($c) { return function (Request $request, Response $response, $exception) use ($c) { return $c['response']->withStatus(500); }; }; $c['notFoundHandler'] = function ($c) { return function (Request $request, Response $response) use ($c) { return $c['response']->withStatus(404); }; }; $app->run();