Yii2 redirect causes blank page

半腔热情 提交于 2019-12-12 19:12:10

问题


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?



来源:https://stackoverflow.com/questions/34829969/yii2-redirect-causes-blank-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!