Renders a Controller and returns the Response content.
Note that this method generates an esi:include tag only when both the standalone option is set to true and the request has ESI capability ( Available options:
string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI:
array $options An array of options:
string The Response content
Symfony\Component\HttpKernel\HttpCache\ESI).
public function render($controller, array $options = array()) {
$options = array_merge(array(
'attributes' => array(),
'query' => array(),
'ignore_errors' => !$this->container
->getParameter('kernel.debug'),
'alt' => array(),
'standalone' => false,
'comment' => '',
), $options);
if (!is_array($options['alt'])) {
$options['alt'] = array(
$options['alt'],
);
}
if (null === $this->esiSupport) {
$this->esiSupport = $this->container
->has('esi') && $this->container
->get('esi')
->hasSurrogateEsiCapability($this->container
->get('request'));
}
if ($this->esiSupport && (true === $options['standalone'] || 'esi' === $options['standalone'])) {
$uri = $this
->generateInternalUri($controller, $options['attributes'], $options['query']);
$alt = '';
if ($options['alt']) {
$alt = $this
->generateInternalUri($options['alt'][0], isset($options['alt'][1]) ? $options['alt'][1] : array(), isset($options['alt'][2]) ? $options['alt'][2] : array());
}
return $this->container
->get('esi')
->renderIncludeTag($uri, $alt, $options['ignore_errors'], $options['comment']);
}
if ('js' === $options['standalone']) {
$uri = $this
->generateInternalUri($controller, $options['attributes'], $options['query'], false);
$defaultContent = null;
if ($template = $this->container
->getParameter('templating.hinclude.default_template')) {
$defaultContent = $this->container
->get('templating')
->render($template);
}
return $this
->renderHIncludeTag($uri, $defaultContent);
}
$request = $this->container
->get('request');
// controller or URI?
if (0 === strpos($controller, '/')) {
$subRequest = Request::create($request
->getUriForPath($controller), 'get', array(), $request->cookies
->all(), array(), $request->server
->all());
if ($session = $request
->getSession()) {
$subRequest
->setSession($session);
}
}
else {
$options['attributes']['_controller'] = $controller;
if (!isset($options['attributes']['_format'])) {
$options['attributes']['_format'] = $request
->getRequestFormat();
}
$options['attributes']['_route'] = '_internal';
$subRequest = $request
->duplicate($options['query'], null, $options['attributes']);
$subRequest
->setMethod('GET');
}
$level = ob_get_level();
try {
$response = $this
->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
if (!$response
->isSuccessful()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request
->getUri(), $response
->getStatusCode()));
}
if (!$response instanceof StreamedResponse) {
return $response
->getContent();
}
$response
->sendContent();
} catch (\Exception $e) {
if ($options['alt']) {
$alt = $options['alt'];
unset($options['alt']);
$options['attributes'] = isset($alt[1]) ? $alt[1] : array();
$options['query'] = isset($alt[2]) ? $alt[2] : array();
return $this
->render($alt[0], $options);
}
if (!$options['ignore_errors']) {
throw $e;
}
// let's clean up the output buffers that were created by the sub-request
while (ob_get_level() > $level) {
ob_get_clean();
}
}
}