本文整理汇总了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;
}