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


PHP View::setStatusCode方法代码示例

本文整理汇总了PHP中FOS\RestBundle\View\View::setStatusCode方法的典型用法代码示例。如果您正苦于以下问题:PHP View::setStatusCode方法的具体用法?PHP View::setStatusCode怎么用?PHP View::setStatusCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在FOS\RestBundle\View\View的用法示例。


在下文中一共展示了View::setStatusCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: getSeccionAction

 public function getSeccionAction($id)
 {
     $view = new View();
     $em = $this->getDoctrine()->getManager();
     $seccion = $em->getRepository('EtsiSiteBundle:Seccion')->find($id);
     if (!$seccion) {
         $view->setStatusCode(404);
     } else {
         $view->setStatusCode(302)->setData($seccion);
     }
     return $this->get('fos_rest.view_handler')->handle($view);
 }
开发者ID:astrakel,项目名称:web,代码行数:12,代码来源:SeccionesController.php

示例2: createView

 protected function createView($returnData)
 {
     $view = new View();
     $view->setData($returnData);
     $view->setStatusCode($returnData['status']);
     $view->setFormat('json');
     return $view;
 }
开发者ID:syedomair,项目名称:SymfonyAPIProject,代码行数:8,代码来源:BaseFOSRestController.php

示例3: getAuthorityAction

 /**
  * @ApiDoc(
  *   resource = true,
  *   description = "Retrieves the health authority data for a postcode",
  *   statusCodes = {
  *     200 = "Returned when successful",
  *     400 = "Returned when there is a data error",
  *     404 = "Returned when no entities are found"
  *   }
  * )
  *
  * @param string $postcode
  * @return View
  */
 public function getAuthorityAction($postcode)
 {
     $result = $this->getService()->findByPostCode(strtolower(str_replace(' ', '', $postcode)));
     $view = new View();
     $view->setData($result);
     if (!count($result)) {
         $view->setStatusCode(404);
     }
     return $view;
 }
开发者ID:TransformCore,项目名称:HayPersistenceApi,代码行数:24,代码来源:AuthorityController.php

示例4: onKernelView

 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     if (!$view instanceof View) {
         if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
             return parent::onKernelView($event);
         }
         $view = new View($view);
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if (null === $view->getStatusCode() && $configuration->getStatusCode()) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         if ($configuration->getSerializerGroups()) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->setGroups($configuration->getSerializerGroups());
             $view->setSerializationContext($context);
         }
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     $viewHandler = $this->container->get('fos_rest.view_handler');
     if ($viewHandler->isFormatTemplating($view->getFormat())) {
         if (!empty($vars)) {
             $parameters = (array) $viewHandler->prepareTemplateParameters($view);
             foreach ($vars as $var) {
                 if (!array_key_exists($var, $parameters)) {
                     $parameters[$var] = $request->attributes->get($var);
                 }
             }
             $view->setData($parameters);
         }
         $template = $request->attributes->get('_template');
         if ($template) {
             if ($template instanceof TemplateReference) {
                 $template->set('format', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $viewHandler->handle($view, $request);
     $event->setResponse($response);
 }
开发者ID:shreyans264,项目名称:symfonyPlayGround,代码行数:59,代码来源:ViewResponseListener.php

示例5: validationFailureAction

 public function validationFailureAction()
 {
     $validator = $this->validator;
     $article = new Article();
     //$article->setPath('/foo');
     $article->setTitle('The path was set');
     $article->setBody('Disable the setPath() call to get a validation error example');
     $view = new View();
     $errors = $validator->validate($article);
     if (count($errors)) {
         $view->setStatusCode(400);
         $view->setData($errors);
     } else {
         $view->setData($article);
     }
     return $this->viewHandler->handle($view);
 }
开发者ID:nashidgit,项目名称:LiipHelloBundle,代码行数:17,代码来源:HelloController.php

示例6: createRedirectResponse

 public function createRedirectResponse(View $view, $location, $format)
 {
     if ($format == 'html' && $view->getData() != null && ($view->getStatusCode() == Codes::HTTP_CREATED || $view->getStatusCode() == Codes::HTTP_ACCEPTED)) {
         $prevStatus = $view->getStatusCode();
         $view->setStatusCode(Codes::HTTP_OK);
     }
     $response = parent::createRedirectResponse($view, $location, $format);
     if ($response->headers->has('Location') && $response->getStatusCode() !== Codes::HTTP_CREATED && ($response->getStatusCode() < 300 || $response->getStatusCode() >= 400)) {
         $response->headers->remove('Location');
     }
     if (isset($prevStatus)) {
         $view->setStatusCode($prevStatus);
         $code = isset($this->forceRedirects[$format]) ? $this->forceRedirects[$format] : $this->getStatusCode($view, $response->getContent());
         $response->setStatusCode($code);
     }
     return $response;
 }
开发者ID:geoffreytran,项目名称:zym,代码行数:17,代码来源:ViewHandler.php

示例7: setContentRangeHeader

 /**
  * @param View $view
  * @param int $offset
  * @param int $limit
  * @param int $total
  */
 protected function setContentRangeHeader(View $view, $offset, $limit, $total)
 {
     $offset = (int) $offset;
     $total = (int) $total;
     $needRange = $limit >= 0 || $offset >= 0;
     if ($needRange) {
         if ($limit === null) {
             $end = $total - 1;
         } else {
             $end = $offset + $limit - 1;
             $end = min($end, $total - 1);
         }
         if ($limit !== null && (int) $limit === 0) {
             $offset = '';
             $end = '';
         }
         $view->setHeader('Content-Range', "items {$offset}-{$end}/{$total}");
     }
     $needPartialContentStatus = $offset > 0 || $limit !== null && $total > $limit;
     if ($needPartialContentStatus) {
         $view->setStatusCode(Response::HTTP_PARTIAL_CONTENT);
     }
 }
开发者ID:glavweb,项目名称:GlavwebRestBundle,代码行数:29,代码来源:GlavwebRestController.php

示例8: onKernelView

 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     /** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     $customViewDefined = true;
     if (!$view instanceof View) {
         if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
             return parent::onKernelView($event);
         }
         $view = new View($view);
         $customViewDefined = false;
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Codes::HTTP_OK === $view->getStatusCode())) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         if ($configuration->getSerializerGroups() && !$customViewDefined) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->setGroups($configuration->getSerializerGroups());
             $view->setSerializationContext($context);
         }
         if ($configuration->getSerializerEnableMaxDepthChecks()) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->enableMaxDepthChecks();
             $view->setSerializationContext($context);
         }
         $populateDefaultVars = $configuration->isPopulateDefaultVars();
     } else {
         $populateDefaultVars = true;
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars && $populateDefaultVars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     $viewHandler = $this->container->get('fos_rest.view_handler');
     if ($viewHandler->isFormatTemplating($view->getFormat())) {
         if (!empty($vars)) {
             $parameters = (array) $viewHandler->prepareTemplateParameters($view);
             foreach ($vars as $var) {
                 if (!array_key_exists($var, $parameters)) {
                     $parameters[$var] = $request->attributes->get($var);
                 }
             }
             $view->setData($parameters);
         }
         $template = $request->attributes->get('_template');
         if ($template) {
             if ($template instanceof TemplateReference) {
                 $template->set('format', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $viewHandler->handle($view, $request);
     $event->setResponse($response);
 }
开发者ID:juliettemijeon,项目名称:BDDProducts,代码行数:70,代码来源:ViewResponseListener.php

示例9: testCreateResponseWithRoute

 public function testCreateResponseWithRoute()
 {
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\Container', array('get'));
     $doRoute = function ($name, $parameters) {
         $route = '/';
         foreach ($parameters as $name => $value) {
             $route .= sprintf('%s/%s/', $name, $value);
         }
         return $route;
     };
     $router = $this->getMockBuilder('Symfony\\Component\\Routing\\RouterInterface')->getMock();
     $router->expects($this->any())->method('generate')->will($this->returnCallback($doRoute));
     $container->expects($this->any())->method('get')->with('fos_rest.router')->will($this->returnValue($router));
     $viewHandler = new ViewHandler(array('json' => false));
     $viewHandler->setContainer($container);
     $view = new View();
     $view->setStatusCode(Codes::HTTP_CREATED);
     $view->setRoute('foo');
     $view->setRouteParameters(array('id' => 2));
     $returnedResponse = $viewHandler->createResponse($view, new Request(), 'json');
     $this->assertEquals('/id/2/', $returnedResponse->headers->get('location'));
 }
开发者ID:mgldev,项目名称:coffeetrack,代码行数:22,代码来源:ViewHandlerTest.php

示例10: testCreateResponseWithRoute

 public function testCreateResponseWithRoute()
 {
     $doRoute = function ($name, $parameters) {
         $route = '/';
         foreach ($parameters as $name => $value) {
             $route .= sprintf('%s/%s/', $name, $value);
         }
         return $route;
     };
     $this->router->expects($this->any())->method('generate')->will($this->returnCallback($doRoute));
     $viewHandler = $this->createViewHandler(['json' => false]);
     $view = new View();
     $view->setStatusCode(Response::HTTP_CREATED);
     $view->setRoute('foo');
     $view->setRouteParameters(['id' => 2]);
     $returnedResponse = $viewHandler->createResponse($view, new Request(), 'json');
     $this->assertEquals('/id/2/', $returnedResponse->headers->get('location'));
 }
开发者ID:sblaut,项目名称:FOSRestBundle,代码行数:18,代码来源:ViewHandlerTest.php

示例11: testStatusCode

 /**
  * @dataProvider statusCodeProvider
  */
 public function testStatusCode($annotationCode, $viewCode, $expectedCode)
 {
     $viewAnnotation = new ViewAnnotation([]);
     $viewAnnotation->setStatusCode($annotationCode);
     $request = new Request();
     $request->setRequestFormat('json');
     $request->attributes->set('_view', $viewAnnotation);
     $this->viewHandler = new ViewHandler(['json' => true]);
     $this->viewHandler->setContainer($this->container);
     // This is why we avoid container dependencies!
     $that = $this;
     $this->container->expects($this->exactly(2))->method('get')->with($this->logicalOr('fos_rest.view_handler', 'fos_rest.templating'))->will($this->returnCallback(function ($service) use($that) {
         return $service === 'fos_rest.view_handler' ? $that->viewHandler : $that->templating;
     }));
     $this->templating->expects($this->any())->method('render')->will($this->returnValue('foo'));
     $view = new View();
     $view->setStatusCode($viewCode);
     $view->setData('foo');
     $event = $this->getResponseEvent($request, $view);
     $response = new Response();
     $event->expects($this->any())->method('setResponse')->will($this->returnCallback(function ($r) use(&$response) {
         $response = $r;
     }));
     $this->listener->onKernelView($event);
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
     $this->assertSame($expectedCode, $response->getStatusCode());
 }
开发者ID:patxi1980,项目名称:FOSRestBundle,代码行数:30,代码来源:ViewResponseListenerTest.php

示例12: putDistrictCouncilDetailsAction

 /**
  * @ApiDoc(
  *   resource = true,
  *   description = "Adds a website the district council data set",
  *   statusCodes = {
  *     200 = "Returned when successful",
  *     400 = "Returned when there is a data error",
  *     404 = "Returned when no entities are found"
  *   }
  * )
  *
  * @param string       $entityCode
  * @param ParamFetcher $paramFetcher
  *
  * @RequestParam(name="url", nullable=false, strict=true, description="The url of the district council website")
  * @RequestParam(name="council_name", nullable=false, strict=true, description="The display name for the disctrict council")
  *
  * @return View
  */
 public function putDistrictCouncilDetailsAction($entityCode, ParamFetcher $paramFetcher)
 {
     /** @var DistrictCouncil $result */
     $result = $this->getService()->findByEntityCode(new DistrictCouncil($entityCode, ''));
     $result->setDistrictCouncilName($paramFetcher->get('council_name'))->setWebsite($paramFetcher->get('url'))->setLastUpdated(new \DateTime());
     $result = $this->getService()->saveDistrictCouncil($result);
     $view = new View();
     $view->setData($result);
     if (null === $result->getId()) {
         $view->setStatusCode(404);
     }
     return $view;
 }
开发者ID:TransformCore,项目名称:HayPersistenceApi,代码行数:32,代码来源:DistrictCouncilController.php

示例13: onKernelView

 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
         return false;
     }
     /** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     $customViewDefined = true;
     if (!$view instanceof View) {
         if (!$configuration && !$this->forceView) {
             return;
         }
         $view = new View($view);
         $customViewDefined = false;
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         $context = $view->getContext();
         if ($configuration->getSerializerGroups() && !$customViewDefined) {
             $context->addGroups($configuration->getSerializerGroups());
         }
         if ($configuration->getSerializerEnableMaxDepthChecks()) {
             $context->setMaxDepth(0);
         }
         $populateDefaultVars = $configuration->isPopulateDefaultVars();
     } else {
         $populateDefaultVars = true;
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars && $populateDefaultVars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     if ($this->viewHandler->isFormatTemplating($view->getFormat()) && !$view->getRoute() && !$view->getLocation()) {
         if (!empty($vars)) {
             $parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
             foreach ($vars as $var) {
                 if (!array_key_exists($var, $parameters)) {
                     $parameters[$var] = $request->attributes->get($var);
                 }
             }
             $view->setData($parameters);
         }
         $template = null !== $configuration && $configuration->getTemplate() ? $configuration->getTemplate() : $request->attributes->get('_template');
         if ($template && !$view->getTemplate()) {
             if ($template instanceof TemplateReferenceInterface) {
                 $template->set('format', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $this->viewHandler->handle($view, $request);
     $event->setResponse($response);
 }
开发者ID:sblaut,项目名称:FOSRestBundle,代码行数:69,代码来源:ViewResponseListener.php

示例14: testStatusCode

 /**
  * @dataProvider statusCodeProvider
  */
 public function testStatusCode($annotationCode, $viewCode, $expectedCode)
 {
     $this->createViewResponseListener(['json' => true]);
     $viewAnnotation = new ViewAnnotation([]);
     $viewAnnotation->setStatusCode($annotationCode);
     $request = new Request();
     $request->setRequestFormat('json');
     $request->attributes->set('_view', $viewAnnotation);
     $this->templating->expects($this->any())->method('render')->will($this->returnValue('foo'));
     $view = new View();
     $view->setStatusCode($viewCode);
     $view->setData('foo');
     $event = $this->getResponseEvent($request, $view);
     $response = new Response();
     $event->expects($this->any())->method('setResponse')->will($this->returnCallback(function ($r) use(&$response) {
         $response = $r;
     }));
     $this->listener->onKernelView($event);
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
     $this->assertSame($expectedCode, $response->getStatusCode());
 }
开发者ID:fsevestre,项目名称:FOSRestBundle,代码行数:24,代码来源:ViewResponseListenerTest.php

示例15: testSetStatusCode

 public function testSetStatusCode()
 {
     $view = new View();
     $code = 404;
     $view->setStatusCode($code);
     $this->assertEquals($code, $view->getStatusCode());
 }
开发者ID:LamaDelRay,项目名称:test_symf,代码行数:7,代码来源:ViewTest.php


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