Yii2 redirect causes blank page

2019-07-08 08:36发布

Is there any server/hosting setting that could cause simple redirect in Yii2 to not work?

For example:

function actionIndex(){
    return $this->redirect('other-page');
}

This code ends up with a blank page, no errors or messages are displayed(Error reporting is on), response code is 200 (although Yii2 debugger shows 302). Everything on my website works fine but as soon as code reaches any ->redirect() it just end up with blank page.

This happens only on my clients shared hosting (on my local machine and development shared hosting everything works just fine). Yii2 runtime logs does not show any errors, neither server logs that my hosting is giving me access to.

3条回答
祖国的老花朵
2楼-- · 2019-07-08 08:57

Put argument in array with an absolute path:

return $this->redirect(['/other-page']);

And check your routes in config, are the same for both environments?

查看更多
一夜七次
3楼-- · 2019-07-08 09:03

It turned out that this was standard white page error: additional new blank line at the beginning of a php file. I went through framework files, line by line. When I commented out if(headers_sent()) {return;} Yii finally threw an exception, only to let me find out that one of my config files had extra new line at the beginning of the file. Removing it fixed the problem.

查看更多
别忘想泡老子
4楼-- · 2019-07-08 09:08

Make sure that no header sent before you send redirect response.

Also check your php file encoding, if you're using UTF-8 make sure that it's UTF-8 encode without BOM.

查看更多
登录 后发表回答