本文整理汇总了PHP中Phalcon\Http\Response::setJsonContent方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::setJsonContent方法的具体用法?PHP Response::setJsonContent怎么用?PHP Response::setJsonContent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\Http\Response
的用法示例。
在下文中一共展示了Response::setJsonContent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: renderAJAX
/**
* Renders the uncaught exception for AJAX consumption.
*
* @param Phalcon\Http\Response $response
* @param \Exception $exceptio
* @return void
*/
protected function renderAJAX(Response $response, Exception $exception)
{
if ($this->config->debug) {
$response->setJsonContent(['_exception' => $exception->getMessage() . ': ' . $exception->getTraceAsString()]);
} else {
$response->setJsonContent(['status' => false]);
}
}
示例2: indexAction
public function indexAction($name)
{
$response = new Response();
$this->getData($name);
if (!$this->result) {
$response->setJsonContent(array('status' => 'NOT-FOUND'));
} else {
$response->setJsonContent(array('status' => 'FOUND', 'data' => $this->result));
}
return $response;
}
示例3: getContent
function getContent($robots)
{
//Create a response
$response = new Response();
$data = array();
foreach ($robots as $robot) {
array_push($data, $robot);
}
if ($data == false || count($data) == 0) {
$response->setJsonContent(array('status' => 'NOT-FOUND'));
} else {
$response->setJsonContent(array('status' => 'FOUND', 'data' => $data));
}
return $response;
}
示例4: jsonResponse
protected function jsonResponse($data)
{
$response = new Response();
$response->setContentType('application/json', 'UTF-8');
$response->setJsonContent($data);
return $response;
}
示例5: returnResult
public function returnResult($result, $errorMessage = null)
{
$response = new Response();
$result = ['result' => $result, 'status' => $errorMessage === null ? self::SUCCESS_RESPONSE : self::FAILED_RESPONSE, 'error_message' => (string) $errorMessage];
$response->setJsonContent($result);
return $response;
}
示例6: sendResult
/**
* 发送数据
* @param mixed $result
*/
private static function sendResult($result)
{
$response = new Response();
$response->setHeader('Content-Type', 'application/json; charset=UTF-8');
$response->setHeader('Access-Control-Allow-Origin', '*');
$response->setJsonContent($result);
$response->send();
exit;
}
示例7: setJsonContent
public function setJsonContent($content, $jsonOptions = 0, $depth = 512)
{
parent::setJsonContent($content, $jsonOptions, $depth);
$this->setHeader('Access-Control-Allow-Origin', '*');
$this->setHeader('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
$this->setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Authorization');
$this->setHeader('E-Tag', md5($this->getContent()));
$this->setContentType('application/json');
}
示例8: sendResponse
public function sendResponse()
{
if ($this->di->has('view')) {
$this->di->get('view')->disable();
}
$response = new Response();
$response->setContentType('application/json', 'utf8');
$response->setJsonContent($this->getResponse());
$response->send();
}
示例9: response
public function response($data = array(), $status = 200, $token = null)
{
$response = new Response();
$response->setStatusCode($status);
$response->setContent(!empty($this->_statuses[$status]) ? $this->_statuses[$status] : null);
$response->setHeader('Content-type', 'application/json');
$response->setHeader('api-version', '1.0');
$response->setHeader('singou-token', $token);
$response->setJsonContent($data);
return $response;
}
示例10: voteAction
/**
* Votes for a poll option
*
* @param int $id Post ID
* @param int $option Option ID
* @return Response
*/
public function voteAction($id = 0, $option = 0)
{
$response = new Response();
if (!$this->checkTokenGetJson('post-' . $id)) {
$csrfTokenError = ['status' => 'error', 'message' => 'This post is outdated. Please try to vote again.'];
return $response->setJsonContent($csrfTokenError);
}
if (!($post = Posts::findFirstById($id))) {
$contentNotExist = ['status' => 'error', 'message' => 'Poll does not exist'];
return $response->setJsonContent($contentNotExist);
}
if (!($user = Users::findFirstById($this->session->get('identity')))) {
$contentlogIn = ['status' => 'error', 'message' => 'You must log in first to vote'];
return $response->setJsonContent($contentlogIn);
}
if (!($option = PostsPollOptions::findFirstById($option))) {
$optionNotFound = ['status' => 'error', 'message' => 'Please select one option from the list below'];
return $response->setJsonContent($optionNotFound);
}
if ($post->isParticipatedInPoll($user->id)) {
$contentAlreadyVote = ['status' => 'error', 'message' => 'You have already voted this post'];
return $response->setJsonContent($contentAlreadyVote);
}
$pollVote = new PostsPollVotes();
$pollVote->posts_id = $post->id;
$pollVote->users_id = $user->id;
$pollVote->options_id = $option->id;
if (!$pollVote->save()) {
foreach ($pollVote->getMessages() as $message) {
/** @var \Phalcon\Mvc\Model\Message $message */
$contentError = ['status' => 'error', 'message' => $message->getMessage()];
return $response->setJsonContent($contentError);
}
}
if ($post->users_id != $user->id) {
$post->user->increaseKarma(Karma::SOMEONE_DID_VOTE_MY_POLL);
$user->increaseKarma(Karma::VOTE_ON_SOMEONE_ELSE_POLL);
}
if (!$post->save()) {
foreach ($post->getMessages() as $message) {
/** @var \Phalcon\Mvc\Model\Message $message */
$contentErrorSave = ['status' => 'error', 'message' => $message->getMessage()];
return $response->setJsonContent($contentErrorSave);
}
}
$viewCache = $this->getDI()->getShared('viewCache');
$viewCache->delete('post-' . $post->id);
$viewCache->delete('poll-votes-' . $post->id);
$viewCache->delete('poll-options-' . $post->id);
$contentOk = ['status' => 'OK'];
return $response->setJsonContent($contentOk);
}
示例11: deleteAction
/**
* Method Http accept: DELETE
*/
public function deleteAction()
{
$modelName = $this->modelName;
$model = $modelName::findFirst($this->id);
//delete if exists the object
if ($model != false) {
if ($model->delete() == true) {
$this->response->setJsonContent(array('status' => "OK"));
} else {
$this->response->setStatusCode(409, "Conflict");
$errors = array();
foreach ($model->getMessages() as $message) {
$errors[] = $this->language[$message->getMessage()] ? $this->language[$message->getMessage()] : $message->getMessage();
}
$this->response->setJsonContent(array('status' => "ERROR", 'messages' => $errors));
}
} else {
$this->response->setStatusCode(409, "Conflict");
$this->response->setJsonContent(array('status' => "ERROR", 'messages' => array("O elemento não existe")));
}
return $this->response;
}
示例12: res
function res($req, $res)
{
$response = new Response();
$response->setContentType('application/json');
switch ($req) {
case 'INDEX':
if ($res == false) {
$response->setJsonContent(array('status' => 'NOT-FOUND'));
} else {
$response->setJsonContent(array('status' => 'FOUND', 'data' => $res));
}
break;
case 'READ':
if ($res == false) {
$response->setJsonContent(array('status' => 'NOT-FOUND'));
} else {
$response->setJsonContent(array('status' => 'FOUND', 'data' => $res));
}
break;
default:
if ($res->success() == true) {
switch ($req) {
case 'CREATE':
$response->setStatusCode(201, 'Created');
break;
case 'UPDATE':
$response->setStatusCode(200, 'Update Successfully');
break;
case 'DELETE':
$response->setStatusCode(205, 'Delete Successfully');
break;
}
$response->setJsonContent(array('status' => 'OK'));
} else {
$response->setStatusCode(409, 'Conflict');
$errors = array();
foreach ($res->getMessages() as $message) {
$errors[] = $message->getMEssage();
}
$response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors));
}
break;
}
return $response;
}
示例13: joinAction
function joinAction($id)
{
$response = new Response();
if (!$id) {
$error = array("status" => "error", "message" => "No id was specified.");
return $response->setJsonContent($error);
}
$league = Leagues::findFirst("id = '{$id}'");
if (!$league) {
$error = array("status" => "error", "message" => "The league does not exist.");
return $response->setJsonContent($error);
}
$uid = $this->session->get('user');
$lid = $league->id;
$userLeague = UserLeagues::findFirst("lid = '{$lid}' AND uid = '{$uid}'");
if ($userLeague) {
$error = array("status" => "error", "message" => "You are already a member of this league.");
return $response->setJsonContent($error);
}
$userLeague = new UserLeagues();
$userLeague->uid = $uid;
$userLeague->lid = $lid;
if (!$userLeague->create()) {
$error = array("status" => "error", "message" => "There was an error creating the league membership.");
return $response->setJsonContent($error);
}
$success = array("status" => "success");
return $response->setJsonContent($success);
}
示例14: Response
} else {
throw new \Exception('É necessário estar registrado no sistema para realizar requisições2.', StatusCodes::NAO_AUTORIZADO);
}
} else {
return true;
}
}
});
$app->setEventsManager($eventsManager);
$app->error(function ($exception) {
$response = new Response();
$response->setContentType('application/json', 'UTF-8');
$response->setStatusCode($exception->getCode());
$response->setHeader("Access-Control-Allow-Origin", "*");
$response->setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
$response->setHeader("Access-Control-Allow-Headers", "Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type");
$response->setJsonContent(['error' => $exception->getMessage()]);
$response->send();
return false;
});
$app->after(function () use($app) {
$return = $app->getReturnedValue();
if ($return instanceof Response) {
$return->send();
} else {
$response = new Response();
$response->setContentType('application/json', 'UTF-8');
$response->setJsonContent($return);
$response->send();
}
});
示例15: response
/**
* @param array $data
* @param int $status
* @return Response
*/
public function response($data = array(), $status = 200)
{
$response = new Response();
$response->setStatusCode($status);
$response->setContent(!empty($this->_statuses[$status]) ? $this->_statuses[$status] : null);
$response->setHeader('Content-type', 'application/json');
$response->setHeader('api-version', '1.0');
$devDebug = $this->request->get('devDebug');
if (true == $devDebug) {
$profiles = $this->getDI()->get('profiler')->getProfiles();
foreach ($profiles as $profile) {
$data['SQL Statement'] = $profile->getSQLStatement();
$data['Start Time'] = $profile->getInitialTime();
$data['Final Time'] = $profile->getFinalTime();
$data['Total Elapsed Time'] = $profile->getTotalElapsedSeconds();
}
}
$response->setJsonContent($data, JSON_PRETTY_PRINT);
return $response;
}