本文整理汇总了PHP中Symfony\Component\HttpFoundation\JsonResponse::setStatusCode方法的典型用法代码示例。如果您正苦于以下问题:PHP JsonResponse::setStatusCode方法的具体用法?PHP JsonResponse::setStatusCode怎么用?PHP JsonResponse::setStatusCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpFoundation\JsonResponse
的用法示例。
在下文中一共展示了JsonResponse::setStatusCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$response = new JsonResponse();
$detail = sprintf("Message: %s\nFile: %s:%s", $exception->getMessage(), $exception->getFile(), $exception->getLine());
$data = ['type' => '#0', 'code' => 0, 'title' => 'Internal Server Error', 'status' => JsonResponse::HTTP_INTERNAL_SERVER_ERROR, 'detail' => $detail];
$response->setStatusCode(JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
$response->setData($data);
if ($exception instanceof HttpExceptionInterface) {
$response->headers->replace($exception->getHeaders());
}
if ($exception instanceof HttpException) {
$data = ['type' => '#' . $exception->getCode(), 'code' => $exception->getCode(), 'title' => $exception->getMessage(), 'status' => JsonResponse::HTTP_BAD_REQUEST, 'detail' => $exception->getDetails()];
$response->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
$response->setData($data);
}
if ($exception instanceof AccessDeniedHttpException) {
$event->setResponse(new Response("", 403));
} else {
$event->setResponse($response);
}
if (!in_array($this->kernel->getEnvironment(), array('dev', 'test'))) {
unset($data['detail']);
}
}
示例2: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$response = new JsonResponse();
$response->setData(['message' => $exception->getMessage()]);
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
}
if ('RuntimeException' === get_class($exception)) {
$response->setStatusCode($exception->getCode());
}
if ($exception instanceof ArrayErrorsException) {
$response->setData(['formErrors' => $exception->getErrors()]);
}
if ($exception instanceof AuthenticationException) {
$response->setData(['message' => $exception->getMessageKey()]);
$response->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
}
if ($exception instanceof InsufficientAuthenticationException) {
$response->setData(['message' => $this->translator->trans('error.insufficient_authentication')]);
$response->setStatusCode(JsonResponse::HTTP_FORBIDDEN);
}
$event->setResponse($response);
}
示例3: onKernelException
/**
* Converts response into json which contains the exception message.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event The response event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
if ($event->getRequest()->getRequestFormat() === 'json') {
$exception = $event->getException();
$response = new JsonResponse();
switch ($exception) {
case $exception instanceof InvalidFormException:
$response->setStatusCode(400);
$response->setData($exception->getFormErrors());
break;
case $exception instanceof \InvalidArgumentException:
$response->setStatusCode(400);
$response->setData(['error' => $exception->getMessage()]);
break;
case $exception instanceof AccessDeniedException:
$response->setStatusCode(403);
$response->setData(['error' => 'Not allowed to access this resource']);
break;
case $exception instanceof NoResultException:
$response->setStatusCode(404);
$response->setData(['error' => 'Does not exist any object with id passed']);
break;
case $exception instanceof ResourceInUseException || $exception instanceof ResourceAlreadyPersistException || $exception instanceof CollectionMinLengthException:
$response->setStatusCode(409);
$response->setData(['error' => $exception->getMessage()]);
break;
default:
$response->setData(['error' => $exception->getMessage()]);
}
$event->setResponse($response);
}
}
示例4: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$response = new JsonResponse(['errorMessage' => $event->getException()->getMessage(), 'stackTrace' => $event->getException()->getTraceAsString()]);
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
} else {
if ($exception instanceof AuthenticationException) {
$response->setData(['errorMessage' => 'You don\'t have permissions to do this.']);
$response->setStatusCode(Response::HTTP_FORBIDDEN);
} else {
if ($exception instanceof InvalidFormException) {
$errors = [];
foreach ($exception->getForm()->getErrors(true) as $error) {
if ($error->getOrigin()) {
$errors[$error->getOrigin()->getName()][] = $error->getMessage();
}
}
$data = ['errors' => $errors, 'errorMessage' => ''];
$response->setData($data);
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
} else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
}
$event->setResponse($response);
}
示例5: thumbnailAction
public function thumbnailAction(Request $request, $id)
{
$mediaRepository = $this->getRepository('BuzzworthyMediaBundle:Media');
$video = $mediaRepository->find($id);
$time = $request->get('time');
$thumbnailManager = $this->get('app.thumbnail_manager')->createThumbnail($video->getWebPath(), $time);
$response = new JsonResponse();
true == $thumbnailManager ? $response->setStatusCode(Response::HTTP_OK) : $response->setStatusCode(Response::HTTP_FORBIDDEN);
return $response;
}
示例6: currentMapAction
/**
* Obteniendo el ultimo mapa guardado
*/
public function currentMapAction()
{
$em = $this->getDoctrine()->getManager();
$mapa = $em->getRepository('AdminBundle:MapaRecorrido')->findCurrentMap();
$response = new JsonResponse();
if ($mapa) {
$response->setData($mapa);
$response->setStatusCode(Response::HTTP_OK);
} else {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
return $response;
}
示例7: onKernelException
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$response = new JsonResponse();
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
} elseif ($exception instanceof AuthenticationException) {
$response->setStatusCode(403);
} else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}
$response->setData(array('message' => $exception->getMessage(), 'status_code' => $response->getStatusCode(), 'trace' => $exception->getTrace(), 'code' => $exception->getCode()));
$event->setResponse($response);
}
示例8: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$response = new JsonResponse();
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
$response->setData(array('message' => $exception->getMessage()));
} else {
$response->setData(array('message' => 'An internal server error occurred. Sorry for the inconvenience.'));
$response->setStatusCode(JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
}
$event->setResponse($response);
}
示例9: transform
/**
* @param \Dafiti\Silex\Response $controllerResponse
*
* @return HttpFoundation\Response
*/
protected function transform(\Dafiti\Silex\Response $controllerResponse)
{
$response = new HttpFoundation\JsonResponse();
$response->headers->add(['Content-Type' => self::CONTENT_TYPE]);
$response->setStatusCode($controllerResponse->getStatusCode());
if ($this->hasError()) {
$content = ['message' => $controllerResponse->getErrorMessage()];
$response->setData($content);
return $response;
}
$response->setStatusCode($controllerResponse->getStatusCode());
$content = $this->getContent($controllerResponse);
$response->setData($content);
return $response;
}
示例10: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
if (!$event->getRequest()->isXmlHttpRequest()) {
return;
}
$exception = $event->getException();
$response = new JsonResponse(array('hasError' => true, 'error' => $exception->getMessage()));
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}
$event->setResponse($response);
}
示例11: on_kernel_exception
/**
* This listener is run when the KernelEvents::EXCEPTION event is triggered
*
* @param GetResponseForExceptionEvent $event
*/
public function on_kernel_exception(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$message = $exception->getMessage();
if ($exception instanceof exception_interface) {
$message = $this->language->lang_array($message, $exception->get_parameters());
}
if (!$event->getRequest()->isXmlHttpRequest()) {
$this->template->assign_vars(array('TITLE' => $this->language->lang('INFORMATION'), 'BODY' => $message));
$response = $this->controller_helper->render('installer_main.html', $this->language->lang('INFORMATION'), false, 500);
} else {
$data = array();
if (!empty($message)) {
$data['message'] = $message;
}
if (defined('DEBUG')) {
$data['trace'] = $exception->getTrace();
}
$response = new JsonResponse($data, 500);
}
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->add($exception->getHeaders());
}
$event->setResponse($response);
}
示例12: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
if ('json' !== $request->getRequestFormat()) {
return;
}
$result = $event->getControllerResult();
if (null === $result) {
$event->setResponse(new JsonResponse(null, 204));
return;
}
if ($result instanceof Response) {
return;
}
$response = new JsonResponse();
if ($request->isMethod('POST')) {
$response->setStatusCode(201);
}
if (!is_scalar($result)) {
$result = $this->getSerializer()->normalize($result, null, ['groups' => $this->options['serialization_default_groups']]);
}
$response->setData($result);
$event->setResponse($response);
}
示例13: saveAction
/**
* Process Ajax Form to create/update virtual product
*
* @param int $idProduct
* @param Request $request
*
* @return string
*/
public function saveAction($idProduct, Request $request)
{
$response = new JsonResponse();
$legacyContext = $this->container->get('prestashop.adapter.legacy.context');
$adminProductWrapper = $this->container->get('prestashop.adapter.admin.wrapper.product');
$productAdapter = $this->container->get('prestashop.adapter.data_provider.product');
//get product
$product = $productAdapter->getProduct((int) $idProduct, true);
if (!$product || !$request->isXmlHttpRequest()) {
return $response;
}
$form = $this->createForm('PrestaShopBundle\\Form\\Admin\\Product\\ProductVirtual', null, array('csrf_protection' => false));
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$res = $adminProductWrapper->updateDownloadProduct($product, $data);
$res->file_download_link = $res->filename ? $legacyContext->getAdminBaseUrl() . $res->getTextLink(true) : '';
$product->is_virtual = 1;
$product->save();
$response->setData($res);
} else {
$response->setStatusCode(400);
$response->setData($this->getFormErrorsForJS($form));
}
return $response;
}
示例14: start
/**
* @inheritdoc
*/
public function start(Request $request, AuthenticationException $authException = null)
{
$response = new JsonResponse();
$response->setStatusCode(401);
$response->setData(['ok' => false, 'error' => 'Accessing this resource requires authorization']);
return $response;
}
示例15: addAction
/**
* Manage form add product attachment
*
* @param int $idProduct
* @param Request $request
*
* @return string
*/
public function addAction($idProduct, Request $request)
{
$response = new JsonResponse();
$legacyContext = $this->container->get('prestashop.adapter.legacy.context');
$adminProductWrapper = $this->container->get('prestashop.adapter.admin.wrapper.product');
$productAdapter = $this->container->get('prestashop.adapter.data_provider.product');
//get product
$product = $productAdapter->getProduct((int) $idProduct);
if (!$product || !$request->isXmlHttpRequest()) {
return $response;
}
$form = $this->createForm('PrestaShopBundle\\Form\\Admin\\Product\\ProductAttachement', null, array('csrf_protection' => false));
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$res = $adminProductWrapper->processAddAttachment($product, $data, $legacyContext->getLanguages());
if ($res) {
$res->real_name = $data['name'];
$response->setData($res);
}
} else {
$response->setStatusCode(400);
$response->setData($this->getFormErrorsForJS($form));
}
return $response;
}