Ejemplo n.º 1
0
 /**
  * Send the Exception message using JSON response 
  *
  * @return HTTP response | true
  */
 public function send()
 {
     $di = \Phalcon\DI::getDefault();
     $res = $di->get('response');
     $req = $di->get('request');
     if (!$req->get('suppress_response_codes', null, null)) {
         $res->setStatusCode($this->getCode(), $this->response)->sendHeaders();
     } else {
         $res->setStatusCode(200, 'OK')->sendHeaders();
     }
     $error = array('httpCode' => $this->getCode(), 'httpError' => $this->getMessage());
     if (!$req->get('type') || $req->get('type') == 'json') {
         $response = new \Modules\Core\Responses\JSONResponse();
         $response->send($error, true);
         return;
     }
     return true;
 }
Ejemplo n.º 2
0
    // OPTIONS have no body, send the headers, exit
    if ($app->request->getMethod() == 'OPTIONS') {
        $app->response->setStatusCode('200', 'OK');
        $app->response->send();
        return;
    }
    $res = $app->getReturnedValue();
    $in = json_encode($res, FALSE);
    if ($in === null) {
        $response = $di->get('response');
        $response->setContent($res);
        $response->send();
        return;
    }
    if (!$app->request->get('type') || $app->request->get('type') == 'json') {
        $response = new \Modules\Core\Responses\JSONResponse();
        $response->send($res);
        return;
    } else {
        throw new \Modules\Core\Exceptions\HTTPException('Could not return results in specified format', 403);
    }
});
/**
 * Default handler when route is not found.
 */
$app->notFound(function () use($app) {
    throw new \Modules\Core\Exceptions\HTTPException('Not Found.', 404);
});
/**
 * Run everything
 */