How to set a header and render a twig template without renderView() method in symfony2.X controller

http-headers, symfony

Solution

You can do it returning the response as rendered view (check this sample)

public function indexAction()
{
   // a parameter which needs to be set in twig
   $variable = 'This is sample assignment';
   $current_user = $this->user; // assume you defined a private variable in your class which contains the current user object

   $response = new Response(
      'AcmeMyBundle:Default:myTemplate.html.twig',
      ['parameter1' => $variable],
      ['user' => $current_user]
   );

   return $response;
}

If your response has a specific header info you can easily set by `$response->header->set(...);`

Problem

How would one go about setting a header (Content Type) and rendering a twig template without renderView() method in symfony2.X controller?

Original source