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(), 'messages' => $this->messages); if (!$req->get('type') || $req->get('type') == 'json') { $response = new \Base\Framework\Responses\JSONResponse(); $response->send($error, true); return; } else { if ($req->get('type') == 'csv') { $response = new \Base\Framework\Responses\CSVResponse(); $response->send(array($error)); return; } } error_log('HTTPException: ' . $this->getFile() . ' at ' . $this->getLine()); return true; }
* The default behavior is to send the Controller's returned value to the client as JSON. * However, by parsing the request querystring's 'type' paramter, it is easy to install * different response type handlers. Below is an alternate csv handler. */ $app->after(function () use($app) { // OPTIONS have no body, send the headers, exit if ($app->request->getMethod() == 'OPTIONS') { $app->response->setStatusCode('200', 'OK'); $app->response->send(); return; } // Respond by default as JSON if (!$app->request->get('type') || $app->request->get('type') == 'json') { // Results returned from the route's controller. All Controllers should return an array $records = $app->getReturnedValue(); $response = new \Base\Framework\Responses\JSONResponse(); $response->useEnvelope(true)->convertSnakeCase(false)->send($records); return; } else { if ($app->request->get('type') == 'csv') { $records = $app->getReturnedValue(); $response = new \Base\Framework\Responses\CSVResponse(); $response->useHeaderRow(true)->send($records); return; } else { throw new \Base\Framework\Exceptions\HTTPException('Could not return results in specified format', 403, array('dev' => 'Could not understand type specified by type 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.