4

I am using the HttpFoundation component in my project without using the full Symfony2 framework. I try to make a RedirectResponse if some credentials are true and redirect the user (like stated in the documentation), but the return statement is not working.

I have:

use Symfony\Component\HttpFoundation\RedirectResponse;

$logged = 1;

if ($logged == 1) {
    $response = new RedirectResponse('http://google.com/');

    return $response;
} else {
    die("not logged");
}

Nothing happens when i execute this. But if I do this instead, I am successfully redirected to Google:

if ($logged == 1) {
    $response = new RedirectResponse('http://google.com/');

    echo $response;
}

Why does this work with echo but not with return? I don't want to use echo in my class libraries.

Any solutions?

cn007b
  • 16,596
  • 7
  • 59
  • 74
Ivan
  • 5,139
  • 11
  • 53
  • 86

1 Answers1

8

Try: $response->send(); instead echo $response;.

cn007b
  • 16,596
  • 7
  • 59
  • 74