/** * Send a fail response if an API call failed * @param $data * @param int $httpStatusCode */ public function sendFail($data, $httpStatusCode = 500) { $this->response->setStatusCode($httpStatusCode, HttpStatusCodes::getMessage($httpStatusCode))->sendHeaders(); $this->response->setJsonContent(['status' => 'fail', 'data' => $data]); if (!$this->response->isSent()) { $this->response->send(); } }
$emails->post('/', 'addEmailAddress'); $emails->put('/{code}/{email}', 'updateEmailAddress'); $emails->get('/{code}/{email}', 'getEmailAddress'); $app->mount($emails); /** * Expose the /v1/orders end point */ $orders = new MicroCollection(); $orders->setHandler('OrderController', true); $orders->setPrefix('/v1/orders'); $orders->post('/', 'addOrder'); $orders->post('/quote', 'getQuote'); $app->mount($orders); /** * CORS Headers to allow the web app to communicate with the web service */ $app->response->setHeader('Access-Control-Allow-Origin', '*'); /** * Not found handler */ $app->notFound(function () use($app) { $app->response->setStatusCode(404, HttpStatusCodes::getMessage(404))->sendHeaders(); $app->response->setContentType('application/json'); $app->response->setJsonContent(['status' => 'error', 'message' => ResponseMessages::METHOD_NOT_IMPLEMENTED, 'code' => 'METHOD_NOT_IMPLEMENTED']); $app->response->send(); }); $app->handle(); } catch (\Exception $e) { $app->response->setStatusCode(500, HttpStatusCodes::getMessage(500))->sendHeaders(); echo json_encode(['status' => 'error', 'message' => ResponseMessages::INTERNAL_SERVER_ERROR, 'code' => 'INTERNAL_SERVER_ERROR', 'ex' => $e->getMessage()]); }