Sf2 RedirectResponse 不工作

Sf2 RedirectResponse not working

我正在使用 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),但是 return 语句不起作用。

我有:

use Symfony\Component\HttpFoundation\RedirectResponse;

$logged = 1;

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

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

执行此操作时没有任何反应。但如果我这样做,我会成功重定向到 Google:

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

    echo $response;
}

为什么这适用于 echo 但不适用于 return?我不想在我的 class 库中使用 echo

有什么解决办法吗?

尝试:$response->send(); 改为 echo $response;