public function HttpKernel::render

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:

  • attributes: An array of request attributes (only when the first argument is a controller)
  • query: An array of request query parameters (only when the first argument is a controller)
  • ignore_errors: true to return an empty string in case of an error
  • alt: an alternative controller to execute in case of an error (can be a controller, a URI, or an array with the controller, the attributes, and the query arguments)
  • standalone: whether to generate an esi:include tag or not when ESI is supported
  • comment: a comment to add when returning an esi:include tag

Parameters

string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI:

array $options An array of options:

Return value

string The Response content

See also

Symfony\Component\HttpKernel\HttpCache\ESI).

File

drupal/core/lib/Drupal/Core/HttpKernel.php, line 104
Definition of Drupal\Core\HttpKernel.

Class

HttpKernel
This HttpKernel is used to manage scope changes of the DI container.

Namespace

Drupal\Core

Code

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'][RouteObjectInterface::ROUTE_OBJECT] = '_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();
    }
  }
}