當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。