/** * 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, Request $request, $format) { $route = $view->getRoute(); $location = $route ? $this->getRouter()->generate($route, (array) $view->getData(), true) : $view->getLocation(); if ($location) { return $this->createRedirectResponse($view, $location, $format); } $content = null; if ($this->isFormatTemplating($format)) { $content = $this->renderTemplate($view, $format); } elseif ($this->serializeNull || null !== $view->getData()) { $serializer = $this->getSerializer($view); if ($serializer instanceof Serializer) { $context = $this->getSerializationContext($view); $content = $serializer->serialize($view->getData(), $format, $context); } else { $content = $serializer->serialize($view->getData(), $format); } } $response = $view->getResponse(); $response->setStatusCode($this->getStatusCode($view, $content)); if (null !== $content) { $response->setContent($content); } if (!$response->headers->has('Content-Type')) { $response->headers->set('Content-Type', $request->getMimeType($format)); } return $response; }