2

If I prepare a response in my controller like so:

$response = new Response();
$response->setContent($this->render('mytemplate.html.twig');
$response->send();

Rather than using:

return $this->return('mytemplate.html.twig');

Then it hasn't actually returned anything... what should I do after $response->send()? Should I just return true?

4
  • Or remove $response->send(); and return $response Commented Nov 14, 2013 at 16:33
  • That prints the headers Commented Nov 14, 2013 at 16:37
  • No it doesn't*. My bad. Commented Nov 14, 2013 at 16:38
  • It does send headers, but it does not stop script execution. Commented Jul 23, 2015 at 14:19

1 Answer 1

3

You must return response

From symfony documentation:

The goal of a controller is always the same: create and return a Response object.

Example:

use Symfony\Component\HttpFoundation\Response;

public function helloAction()
{
    return new Response('Hello world!');
}

Another example:

use Symfony\Component\HttpFoundation\Response;

$content = $this->renderView(
    'AcmeHelloBundle:Hello:index.html.twig',
    array('name' => $name)
);

return new Response($content);

You can read about it here: http://symfony.com/doc/current/book/controller.html

Sign up to request clarification or add additional context in comments.

2 Comments

This is correct, thanks. I was using render and not renderView when setting my response's content, so it kept printing the headers when I returned the response.
You don't have to return a response if you use the @Template() annotation - then all you need to return is an array of view data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.