如題埠戳,當使用redirect時,拋出空白頁面,無任何錯誤提示怨绣。這個問題隱藏的比較深角溃,使用百度也幾乎搜不到什么有效的解決方案,因此撰寫該文篮撑。
該問題可能是由某一個配置文件的首行出現(xiàn)空行導致减细。空白行導致header提前被發(fā)送赢笨。具體的過程在yii\web\Response
中:
/**
* Sends the response headers to the client
*/
protected function sendHeaders()
{
if (headers_sent()) {
return;
}
if ($this->_headers) {
$headers = $this->getHeaders();
foreach ($headers as $name => $values) {
$name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
// set replace for first occurrence of header but false afterwards to allow multiple
$replace = true;
foreach ($values as $value) {
header("$name: $value", $replace);
$replace = false;
}
}
}
$statusCode = $this->getStatusCode();
header("HTTP/{$this->version} {$statusCode} {$this->statusText}");
$this->sendCookies();
}
該函數(shù)的
if (headers_sent()) {
return;
}
指的是如果header被發(fā)送就返回空白未蝌,即我們所看到的空白頁面。需要具體定位是哪里導致的該錯誤茧妒,可直接將這一小段代碼注釋掉萧吠,然后運行程序,即可看到是哪一處導致桐筏。如何修改也顯而易見了纸型。Good Luck。