/** * This methods will be called at application startup * @param $appInstance * @return void */ public static function addRouteDefinitions(Slim $appInstance) { $appInstance->post('/ajax', function () use(&$appInstance) { $exceptionContentType = 'text/plain'; $appInstance->response->headers->set('Cache-Control', 'no-store'); try { $contentType = EmaRpcApi::slimCallback($appInstance); $appInstance->response->headers->set('Content-Type', $contentType); } catch (SecurityException $e) { $appInstance->response->setStatus(401); $appInstance->response->headers->set('Content-Type', $exceptionContentType); print "Unauthorized.\n" . $e->getMessage(); } catch (\RuntimeException $e) { $appInstance->response->setStatus(400); $appInstance->response->headers->set('Content-Type', $exceptionContentType); print $e->getMessage(); $logger = new DbLogger(); $logger->writeException($e); } catch (\Exception $e) { $logger = new DbLogger(); $logger->writeException($e); $appInstance->response->setStatus(500); $appInstance->response->headers->set('Content-Type', $exceptionContentType); $msg = "Server Error Occurred. Please contact us. Error code is: " . $e->getCode(); if (EMA_DEBUG === true) { $msg = $e->getMessage() . ";\n Code: " . $e->getCode() . "\n\n\n" . $e->getTraceAsString(); } print $msg; } }); if (EMA_REST_API) { $appInstance->map('/rest/:path+', function ($path) use($appInstance) { $appInstance->response->headers->set('Cache-Control', 'no-store'); $appInstance->response->headers->set('Content-Type', 'application/json'); $printoutError = function (\Exception $e, $status = 500) use($appInstance) { $appInstance->response->setStatus($status); print EmaRestApi::getErrorOutput($e); }; try { $rpc = EmaRestApi::rpcFactory($path, $appInstance->request->getMethod(), $appInstance); $result = EmaRestApi::rpcCheckAndRun($rpc, $appInstance); if (EmaRestApi::$isAddition) { $appInstance->response->setStatus(201); $appInstance->response->headers->set('Location', EmaRestApi::$additionRouteBase); } print json_encode($result); } catch (InputError $e) { $printoutError($e, 400); } catch (SecurityException $e) { $printoutError($e, 403); } catch (NotFound $e) { $printoutError($e, 404); } catch (Unsupported $e) { $printoutError($e, 415); } catch (\Exception $e) { $printoutError($e, 500); } })->via('GET', 'POST', 'DELETE'); } }