本文整理汇总了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);
}
示例2: createView
protected function createView($returnData)
{
$view = new View();
$view->setData($returnData);
$view->setStatusCode($returnData['status']);
$view->setFormat('json');
return $view;
}
示例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;
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
}
示例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);
}
示例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'));
}
示例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'));
}
示例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());
}
示例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;
}
示例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);
}
示例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());
}
示例15: testSetStatusCode
public function testSetStatusCode()
{
$view = new View();
$code = 404;
$view->setStatusCode($code);
$this->assertEquals($code, $view->getStatusCode());
}