脚本宝典收集整理的这篇文章主要介绍了在beforeAction中渲染视图时,在Yii中获取“已发送标头”错误,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
class SiteController extends \app\components\Controller { public function beforeAction($action) { // Makes some checks and if it's true,will render a file and stop execution of any action if (...) echo $this->render('standby'); return false; } return true; } // All my other actions here }
这似乎运行良好,并停止执行,但我得到了渲染()行的“已发送标题”,就好像它正在进行重定向.
如果我写Yii :: $app-end()而不是返回false,则会发生同样的事情.
如果我写exit();而不是返回false,没有异常显示,但调试面板不显示,因为Yii没有正确终止.
我尝试删除echo $this-> render(..)并且它导致一个空页面,没有任何重定向,这似乎只是Yii抱怨我从Controller回应的东西.
当然我不能返回render()的结果或返回true,因为它将执行页面的动作,我试图避免并在此结束.
我知道在beforeAction()中返回false会触发EVENT_BEFORE_ACTION,但我看不到我应该在哪里使用它. events documentation并没有真正帮助我.
那么有没有办法显示“待机”视图,阻止其他操作执行,并避免错误消息从控制器回显?
请注意,我正在努力完成这项工作,而不必在每个操作方法中重复代码,以检查beforeAction()的结果是否为false.
public function beforeAction($action) { // Makes some checks and if it's true,will render a file and stop execution of any action if (...) { Yii::$app->response->content = $this->render('standby'); Yii::$app->response->statusCode = 403; // use real HTTP status code here return false; } return parent::beforeAction($action); }
不要忘记调用parent :: beforeAction($action) – 省略它将导致意外且难以调试的行为.
以上是脚本宝典为你收集整理的在beforeAction中渲染视图时,在Yii中获取“已发送标头”错误全部内容,希望文章能够帮你解决在beforeAction中渲染视图时,在Yii中获取“已发送标头”错误所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。