Пример #1
0
 public function send()
 {
     $di = \Phalcon\DI::getDefault();
     $res = $di->get('response');
     $req = $di->get('request');
     //query string, filter, default
     if (!$req->get('suppress_response_codes', null, null)) {
         $res->setStatusCode($this->getCode(), $this->response)->sendHeaders();
     } else {
         $res->setStatusCode('200', 'OK')->sendHeaders();
     }
     $error = array('errorCode' => $this->getCode(), 'userMessage' => $this->getMessage(), 'devMessage' => $this->devMessage, 'more' => $this->additionalInfo, 'applicationCode' => $this->errorCode);
     if (!$req->get('type') || $req->get('type') == 'json') {
         $response = new \Api\Responses\JSONResponse();
         $response->send($error, true);
         return;
     } else {
         if ($req->get('type') == 'csv') {
             $response = new \Api\Responses\CSVResponse();
             $response->send(array($error));
             return;
         }
     }
     error_log('HTTPException: ' . $this->getFile() . ' at ' . $this->getLine());
     return true;
 }
Пример #2
0
        // Results returned from the route's controller.  All Controllers should return an array
        $records = $app->getReturnedValue();
        $response = new \Api\Responses\JSONResponse();
        $response->useEnvelope(true)->convertSnakeCase(false)->send($records);
        return;
    } else {
        if ($app->request->get('format') == 'javascript') {
            // Results returned from the route's controller.  All Controllers should return an array
            $records = $app->getReturnedValue();
            $response = new \Api\Responses\JavascriptResponse();
            $response->useEnvelope(true)->convertSnakeCase(false)->send($records);
            return;
        } else {
            if ($app->request->get('format') == 'csv') {
                $records = $app->getReturnedValue();
                $response = new \Api\Responses\CSVResponse();
                $response->useHeaderRow(true)->send($records);
                return;
            } else {
                throw new \Api\Exceptions\HTTPException('Could not return results in specified format', 403, array('dev' => 'Could not understand format specified by format paramter in query string.', 'internalCode' => 'NF1000', 'more' => 'Type may not be implemented. Choose either "csv" or "json"'));
            }
        }
    }
});
/**
 * The notFound service is the default handler function that runs when no route was matched.
 * We set a 404 here unless there's a suppress error codes.
 */
$app->notFound(function () use($app) {
    throw new \Api\Exceptions\HTTPException('Not Found.', 404, array('dev' => 'That route was not found on the server.', 'internalCode' => 'NF1000', 'more' => 'Check route for mispellings.'));
});