本文整理匯總了PHP中Symfony\Component\HttpFoundation\JsonResponse::setCharset方法的典型用法代碼示例。如果您正苦於以下問題:PHP JsonResponse::setCharset方法的具體用法?PHP JsonResponse::setCharset怎麽用?PHP JsonResponse::setCharset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\Component\HttpFoundation\JsonResponse
的用法示例。
在下文中一共展示了JsonResponse::setCharset方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: getAction
/**
* Display one member identified by id in Json format.
*
* @example
* {
* "member": {
* "id": 1,
* "name": "Bland",
* "firstName": "\u00a0Angie",
* "phone": "0611111111"
* }
* }
*
* @Route("/api/members/{id}", requirements={"id" = "\d+"})
*/
public function getAction($id)
{
$response = new JsonResponse();
$response->setCharset('UTF-8');
$response->setEncodingOptions(JSON_PRETTY_PRINT);
try {
$this->initialize();
$member = $this->memberRepository->findById($id);
if ($member == null) {
$response->setData('Aucun adhérent ne correspond à votre demande');
$response->setStatusCode(JsonResponse::HTTP_NOT_FOUND);
} else {
$response->setData($this->memberFormatter->format($member));
}
} catch (\Exception $e) {
$response->setStatusCode(JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
}
return $response;
}
示例2: jsonSuccess
/**
* Create a JSON response
* @param mixed $data Data to be returned
* @param int $status HTTP response code
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function jsonSuccess($data = null, $status = 200)
{
$data = ['data' => $data];
$r = new JsonResponse($data, $status);
$r->headers->set('Content-Type', 'application/json; charset=utf-8');
$r->setCharset('UTF-8');
return $r;
}