本文整理汇总了PHP中Symfony\Component\HttpFoundation\JsonResponse::setClientTtl方法的典型用法代码示例。如果您正苦于以下问题:PHP JsonResponse::setClientTtl方法的具体用法?PHP JsonResponse::setClientTtl怎么用?PHP JsonResponse::setClientTtl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpFoundation\JsonResponse
的用法示例。
在下文中一共展示了JsonResponse::setClientTtl方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: buildHttpResponse
/**
* Build a JsonResponse based on an API response object
*
* @param AjaxResponse $api_response The API Response
* @param bool $allow_removing_headers Alter PHP's global headers to allow caching
*
* @return JsonResponse
* @throws \RuntimeException
*/
private function buildHttpResponse(AjaxResponse $api_response, $allow_removing_headers = true)
{
if ($api_response->isCancelled()) {
return new JsonResponse(['error' => "The response was cancelled"], 400);
}
$response = new JsonResponse(['msgs' => (object) $this->msgs->dumpRegister(), 'data' => $api_response->getData()]);
$ttl = $api_response->getTtl();
if ($ttl > 0) {
// Required to remove headers set by PHP session
if ($allow_removing_headers) {
header_remove('Expires');
header_remove('Pragma');
header_remove('Cache-Control');
}
// JsonRequest sets a default Cache-Control header we don't want
$response->headers->remove('Cache-Control');
$response->setClientTtl($ttl);
// if we don't set Expires, Apache will add a far-off max-age and Expires for us.
$response->headers->set('Expires', gmdate('D, d M Y H:i:s \\G\\M\\T', time() + $ttl));
}
return $response;
}