处理另一个错误时发生错误:yii \ web \ HeadersAlreadySentException [英] An Error occurred while handling another error: yii\web\HeadersAlreadySentException

查看:607
本文介绍了处理另一个错误时发生错误:yii \ web \ HeadersAlreadySentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对基于Yii2 Framework的留言簿应用程序发表评论.在PC上的localhost上,一切正常,但是在共享主机上,当我想在View中提交评论时,出现此错误.

I am trying to submit a comment on a guestbook application based on Yii2 Framework. on localhost on my PC works everything fine but on the shared hosting when I want to submit a comment in View, I get this error.

这是错误:

An Error occurred while handling another error:
    exception 'yii\web\HeadersAlreadySentException' with message 'Headers already sent in /home/mahdikas/public_html/guestbook/controllers/PostController.php on line 117.' in /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php:366
    Stack trace:
    #0 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
    #1 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response->send()
    #2 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))
    #3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))
    #4 {main}
    Previous exception:
    exception 'yii\web\HeadersAlreadySentException' with message 'Headers already sent in /home/mahdikas/public_html/guestbook/controllers/PostController.php on line 117.' in /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php:366
    Stack trace:
    #0 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
    #1 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response->send()
    #2 /home/mahdikas/public_html/guestbook/web/index.php(12): yii\base\Application->run()
    #3 {main}

在postController中,我有以下代码:

in the postController I have this code:

public function actionAdd_comment()
{
  //print_r($_POST);
  $model = new \app\models\Comments;
  if ($model->load(Yii::$app->request->post()) && $model->validate()) {
    $model->comment_date = date('Y-m-d H:i:s');
    if ($model->save()) {
      echo 'Thanks for your comment.';
    } else {
      echo 'Failed!';
    }
  }
}

错误中的第117行是:

which line 117 in the error is:

echo 'Thanks for your comment.';

您能帮我解决这个问题吗?我真的被困住了.

Can you please help me to solve this problem? I am truly stuck.

预先感谢

推荐答案

从Yii 2.0.14开始,您无法在控制器中回显.必须返回响应:

Since Yii 2.0.14 you cannot echo in controller. Response must be returned:

public function actionAdd_comment() {
    $model = new \app\models\Comments();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $model->comment_date = date('Y-m-d H:i:s');
        if ($model->save()) {
            return 'Thanks for your comment.';
        } else {
            return 'Failed!';
        }
    }
}


如果您无法避免回显,也可以在方法末尾调用exit,以防止进一步处理或将代码包装为ob_start()ob_get_clean().


You may also call exit at the end of your method to prevent further processing or wrap your code with ob_start() and ob_get_clean(), if you're not able to avoid echo.

public function actionAdd_comment() {
    $model = new \app\models\Comments();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $this->someMagicWithEcho();
        exit;
    }
}

public function actionAdd_comment() {
    $model = new \app\models\Comments();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        ob_start();
        $this->someMagicWithEcho();
        return ob_get_clean();
    }
}

这篇关于处理另一个错误时发生错误:yii \ web \ HeadersAlreadySentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆