Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelView PHP Method

onKernelView() public method

Renders the template and initializes a new response object with the rendered template content.
public onKernelView ( GetResponseForControllerResultEvent $event )
$event Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent A GetResponseForControllerResultEvent instance
    public function onKernelView(GetResponseForControllerResultEvent $event)
    {
        $request = $event->getRequest();
        $parameters = $event->getControllerResult();

        if (null === $parameters) {
            if (!$vars = $request->attributes->get('_template_vars')) {
                if (!$vars = $request->attributes->get('_template_default_vars')) {
                    return;
                }
            }

            $parameters = array();
            foreach ($vars as $var) {
                $parameters[$var] = $request->attributes->get($var);
            }
        }

        if (!is_array($parameters)) {
            return $parameters;
        }

        if (!$template = $request->attributes->get('_template')) {
            return $parameters;
        }

        $event->setResponse(new Response($this->container->get('templating')->render($template, $parameters)));
    }

Usage Example

 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     if (!$view instanceof View) {
         if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
             return parent::onKernelView($event);
         }
         $view = new View($view);
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if (null === $view->getStatusCode() && $configuration->getStatusCode()) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         if ($configuration->getSerializerGroups()) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->setGroups($configuration->getSerializerGroups());
             $view->setSerializationContext($context);
         }
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     $viewHandler = $this->container->get('fos_rest.view_handler');
     if ($viewHandler->isFormatTemplating($view->getFormat())) {
         if (!empty($vars)) {
             $parameters = (array) $viewHandler->prepareTemplateParameters($view);
             foreach ($vars as $var) {
                 if (!array_key_exists($var, $parameters)) {
                     $parameters[$var] = $request->attributes->get($var);
                 }
             }
             $view->setData($parameters);
         }
         $template = $request->attributes->get('_template');
         if ($template) {
             if ($template instanceof TemplateReference) {
                 $template->set('format', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $viewHandler->handle($view, $request);
     $event->setResponse($response);
 }
All Usage Examples Of Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelView