Symfony\Bundle\FrameworkBundle\HttpKernel::render PHP 메소드

render() 공개 메소드

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 (@see Symfony\Component\HttpKernel\HttpCache\ESI). 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
public render ( string $controller, array $options = [] ) : string
$controller string A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
$options array An array of options
리턴 string The Response content
    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 && $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']);
        }

        $request = $this->container->get('request');

        // controller or URI?
        if (0 === strpos($controller, '/')) {
            $subRequest = Request::create($controller, 'get', array(), $request->cookies->all(), array(), $request->server->all());
            $subRequest->setSession($request->getSession());
        } else {
            $options['attributes']['_controller'] = $controller;
            $options['attributes']['_format'] = $request->getRequestFormat();
            $options['attributes']['_route'] = '_internal';
            $subRequest = $request->duplicate($options['query'], null, $options['attributes']);
        }

        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()));
            }

            return $response->getContent();
        } 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;
            }
        }
    }

Usage Example

예제 #1
0
 public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
 {
     if (version_compare(phpversion(), '5.3.3', '<')) {
         $this->markTestSkipped('Test fails with PHP 5.3.2 due to https://bugs.php.net/bug.php?id=50563');
     }
     $request = new Request();
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $container->expects($this->at(0))->method('getParameter')->with($this->equalTo('kernel.debug'))->will($this->returnValue(false));
     $container->expects($this->at(1))->method('has')->with($this->equalTo('esi'))->will($this->returnValue(false));
     $container->expects($this->at(2))->method('get')->with($this->equalTo('request'))->will($this->returnValue($request));
     $dispatcher = new EventDispatcher();
     $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
     $resolver->expects($this->once())->method('getController')->will($this->returnValue(function () {
         ob_start();
         echo 'bar';
         throw new \RuntimeException();
     }));
     $resolver->expects($this->once())->method('getArguments')->will($this->returnValue(array()));
     $kernel = new HttpKernel($dispatcher, $container, $resolver);
     // simulate a main request with output buffering
     ob_start();
     echo 'Foo';
     // simulate a sub-request with output buffering and an exception
     $kernel->render('/');
     $this->assertEquals('Foo', ob_get_clean());
 }
All Usage Examples Of Symfony\Bundle\FrameworkBundle\HttpKernel::render