public static function Json($json) { $response = new \Phalcon\Http\Response(); $response->setJsonContent($json); $contentLength = strlen($response->getContent()); $response->setHeader('ContentLength', $contentLength); $response->send(); die; }
public static function p($status = 1, $msg = null, $data = [], $code = 200) { if (method_exists($msg, 'getMessage')) { $msg = $msg->getMessage(); } $response = new \Phalcon\Http\Response(); $response->setHeader("Content-Type", "application/json"); if ($status) { $response->setStatusCode(200); } else { $response->setStatusCode($code); } $response->setJsonContent(array('status' => $status == 1 ? 'success' : 'error', 'msg' => $msg, 'data' => $data)); $response->send(); }
/** * @param $data response to send back as JSON with Callback * @param bool|true $success * @param int $status_code of response default 200 * @param string $status_message of response default OK */ public function response($data, $success = true, $status_code = 200, $status_message = "OK") { //new response $response = new \Phalcon\Http\Response(); //header('Access-Control-Allow-Origin: *'); $response->setStatusCode($status_code, $status_message); $response->setContentType('application/json', 'utf-8'); $response->setHeader('Access-Control-Allow-Origin', '*'); //encode call $json = json_encode(array('success' => $success, 'data' => $data)); //set response to send back check for callback $response->setContent(isset($_GET['callback']) ? "{$_GET['callback']}({$json})" : $json); $response->send(); exit; //kill from other processing }
public static function responseJson(array $data = array(), $state = 200) { $datetime = gmdate("D, d M Y H:i:s") . ' GMT'; $response = new \Phalcon\Http\Response(); $response->setHeader('Pragma', 'no-cache'); $response->setHeader('Cache-Control', 'no-cache, private, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0, max-stale=0'); $response->setHeader('Last-Modified', $datetime); $response->setHeader('X-Frame-Options', 'SAMEORIGIN'); $response->setContentType('application/json', 'UTF-8'); $response->setExpires(new \DateTime()); $response->setStatusCode($state); $response->setEtag(md5($datetime)); $response->setJsonContent($data, JSON_UNESCAPED_UNICODE); $response->send(); // Force quit the dispatcher loop. exit; }
public function indexAction() { $response = new \Phalcon\Http\Response(); $response->redirect("index"); $response->send(); }
$onlytestnet = ""; } $sql = sprintf("UPDATE cmd_nodes SET VersionID=%d WHERE KeepUpToDate=1{$onlytestnet}", $versionid); if ($result = $mysqli->query($sql)) { $info2 = $mysqli->info; if (is_null($info2)) { $info2 = true; } } else { $info2 = $mysqli->errno . ': ' . $mysqli->error; } //Change the HTTP status $response->setStatusCode(202, "Accepted"); $response->setJsonContent(array('status' => 'OK', 'data' => array('VersionId' => $versionid, "KeepUpToDate" => $info2))); } else { $response->setStatusCode(503, "Service Unavailable"); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array($mysqli->errno . ': ' . $mysqli->error))); } } return $response; }); // ============================================================================ // End-point not found // ============================================================================ $app->notFound(function () use($app) { $response = new Phalcon\Http\Response(); $response->setStatusCode(404, "Not Found"); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array('Unknown end-point'))); $response->send(); }); $app->handle();
/** * Main run block that executes the micro application * */ public function run() { // Handle any routes not found $this->notFound(function () { $response = new \Phalcon\Http\Response(); $response->setStatusCode(404, 'Not Found')->sendHeaders(); $response->setContent('Page doesn\'t exist.'); $response->send(); }); $this->handle(); }
public function runException(\Exception $ex) { $response = new \Phalcon\Http\Response(); $response->setStatusCode(400, 'Bad Request'); $response->send(); }
/** * Render the exception. * * @param mixed $e * @param int $status_code * @return mixed */ public function render($e, $status_code = null) { if (is_cli()) { dd(['message' => $e->getMessage(), 'code' => $e->getCode(), 'file' => $e->getFile(), 'line' => $e->getLine()]); return; } $content = (new ExceptionHandler(config()->app->debug))->getHtml($e); if (di()->has('response')) { $response = di()->get('response'); } else { $response = new \Phalcon\Http\Response(); } if (method_exists($e, 'getStatusCode')) { $response->setStatusCode($e->getStatusCode()); } # here, if the $status_code is not empty # that means we should over-ride the current status code # using the provided one if ($status_code) { $response->setStatusCode($status_code); } $response->setContent($content); return $response->send(); }
/** * Run the application. * * @throws Exception Service exceptions that have bubbled to the surface. * * @return void */ public function run() { // Capture runtime errors register_shutdown_function(['\\Eyas\\Utilities\\Debug\\PhpError', 'runtimeShutdown']); try { // Record any php warnings/errors set_error_handler(['\\Eyas\\Utilities\\Debug\\PhpError', 'errorHandler']); // Setup App (dependency injector, configuration variables and autoloading resources/classes) $this->setConfig($this->getConfigPath() . '/' . $this->getConfigFileName()); // Get Authentication Headers $clientId = $this->request->getHeader('API_ID'); $time = $this->request->getHeader('API_TIME'); $hash = $this->request->getHeader('API_HASH'); $privateKey = \Eyas\Mvc\Model\Api::findFirst(array('public_id' => $clientId))->private_key; $data = (array) $this->request->getJsonRawBody(); if (isset($data['_url'])) { // remove _url from the user submitted data unset($data['_url']); } $message = new \Eyas\Micro\Messages\Auth($clientId, $time, $hash, $data); // Setup HMAC Authentication callback to validate user before routing message // Failure to validate will stop the process before going to proper Restful Route $this->setEvents(new \Eyas\Events\Api\HmacAuthenticate($message, $privateKey)); // Setup RESTful Routes $this->setRoutes($this->getConfigPath() . '/' . $this->getRoutesFileName()); // Handle any routes not found $this->notFound(function () { $response = new \Phalcon\Http\Response(); $response->setStatusCode(501, 'Not Implemented')->sendHeaders(); $response->setContent('The method requested has not been implemented.' . ' It may be in the future.'); $response->send(); }); $this->handle(); } catch (\Exception $e) { // Do Something I guess, return Server Error message $this->response->setStatusCode(500, "Server Error"); $this->response->setContent($e->getMessage()); $this->response->send(); } }
public function beforeDispatch(Event $event, Dispatcher $dispatcher) { if ($this->_status) { $auth = $this->session->get('auth'); if (!$auth) { $role = 'Guests'; } else { $role = $auth['role']; } $controller = $dispatcher->getControllerName(); $action = $dispatcher->getActionName(); $acl = $this->getAcl(); $allowed = $acl->isAllowed($role, $controller, $action); if ($allowed != Acl::ALLOW) { // Getting a response instance $response = new \Phalcon\Http\Response(); $response->redirect('/user/login', true); $response->send(); return false; } } else { return true; } }