Yii2 redirect causes blank page

2019-07-08 08:46发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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?