Ivan
Ivan

Reputation: 5238

Sf2 RedirectResponse not working

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?

Upvotes: 4

Views: 1092

Answers (1)

cn0047
cn0047

Reputation: 17091

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

Upvotes: 8

Related Questions