当前位置: 首页>>代码示例>>PHP>>正文


PHP JsonResponse::setStatusCode方法代码示例

本文整理汇总了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']);
     }
 }
开发者ID:reisraff,项目名称:symfony-api-util,代码行数:25,代码来源:ExceptionListener.php

示例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);
 }
开发者ID:CawaKharkov,项目名称:symfony2-makeup-blog,代码行数:27,代码来源:ExceptionListener.php

示例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);
     }
 }
开发者ID:cespedosa,项目名称:kreta,代码行数:37,代码来源:JsonExceptionListener.php

示例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);
 }
开发者ID:Gardax,项目名称:cookWithMeAPI,代码行数:28,代码来源:ExceptionListener.php

示例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;
 }
开发者ID:romankd,项目名称:oro-code-example,代码行数:10,代码来源:VideoController.php

示例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;
 }
开发者ID:ricky6982,项目名称:tesisApp,代码行数:16,代码来源:MapaController.php

示例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);
 }
开发者ID:kmlkrks,项目名称:restapi-example,代码行数:17,代码来源:ExceptionListener.php

示例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);
 }
开发者ID:Pamoi,项目名称:photo-gallery-api,代码行数:14,代码来源:ExceptionListener.php

示例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;
 }
开发者ID:dafiti,项目名称:contentnegotiation-service-provider,代码行数:20,代码来源:Json.php

示例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);
 }
开发者ID:avanthay,项目名称:timekipr,代码行数:15,代码来源:ExceptionListener.php

示例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);
 }
开发者ID:phpbb,项目名称:phpbb-core,代码行数:31,代码来源:kernel_exception_subscriber.php

示例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);
 }
开发者ID:chrisyue,项目名称:auto-json-response-bundle,代码行数:27,代码来源:AutoJsonResponseListener.php

示例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;
 }
开发者ID:M03G,项目名称:PrestaShop,代码行数:34,代码来源:VirtualProductController.php

示例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;
 }
开发者ID:treehouselabs,项目名称:keystone-bundle,代码行数:10,代码来源:KeystoneEntryPoint.php

示例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;
 }
开发者ID:M03G,项目名称:PrestaShop,代码行数:34,代码来源:AttachementProductController.php


注:本文中的Symfony\Component\HttpFoundation\JsonResponse::setStatusCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。