/** * @param ViewHandler $viewHandler * @param View $view * @param Request $request * @param string $format * * @return Response */ public function handleExtension(ViewHandler $handler, View $view, Request $request, $format) { if (in_array("application/vnd.bpi.api+xml", $request->getAcceptableContentTypes())) { $view->setHeader("Content-Type", "application/vnd.bpi.api+xml"); } return $handler->createResponse($view, $request, "xml"); }
/** * @return View */ public function build() { if ($this->setVersion && $this->singleDocument != null) { $this->view->setHeader('ETag', hash('sha256', $this->singleDocument->getId() . $this->singleDocument->getVersion())); } return $this->view; }
/** * @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); } }
/** * Handles creation of a Response using either redirection or the templating/serializer service * * @param View $view * @param Request $request * @param string $format * * @return Response */ public function createResponse(View $view, Request $request, $format) { $headers = $view->getHeaders(); if (empty($headers['Content-Type'])) { $view->setHeader('Content-Type', $request->getMimeType($format)); } $route = $view->getRoute(); $location = $route ? $this->getRouter()->generate($route, (array) $view->getData(), true) : $view->getLocation(); if ($location) { return $this->createRedirectResponse($view, $location, $format); } if ($this->isFormatTemplating($format)) { $content = $this->renderTemplate($view, $format); } else { $serializer = $this->getSerializer($view); $content = $serializer->serialize($view->getData(), $format); } return new Response($content, $this->getStatusCode($view), $view->getHeaders()); }
/** * Creates a new fly from the submitted data. * * @throws AccessDeniedException * * @ApiDoc( * resource = true, * input = "Flyaround\DefaultBundle\Form\Type\FlyType", * statusCodes = { * 200 = "Returned when successful", * 400 = "Returned when the form has errors" * } * ) * * @Annotations\View( * template = "FlyaroundDefaultBundle:Fly:newFly.html.twig", * statusCode = Codes::HTTP_BAD_REQUEST * ) * * @param Request $request the request object * * @return FormTypeInterface|RouteRedirectView */ public function postFlyAction(Request $request) { $em = $this->getDoctrine()->getManager(); $fly = new Fly(); $form = $this->createForm(new FlyType(), $fly); $form->submit($request); if ($form->isValid()) { $em->persist($fly); $em->flush(); return new Response('{"id": ' . $fly->getId() . '}', 201, array('Access-Control-Allow-Origin' => '*')); } $view = new View(array('form' => $form)); $view->setHeader('Access-Control-Allow-Origin', '*'); $group = $this->container->get('security.context')->isGranted('ROLE_API') ? 'restapi' : 'standard'; $view->getSerializationContext()->setGroups(array('Default', $group)); return $view; }