Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

php - "An error occurred while handling another error: yiiwebHeadersAlreadySentException"

I am trying to submit a comment on a guestbook application based on the Yii 2 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.

Here is the error:

An error occurred while handling another error:
    exception 'yiiwebHeadersAlreadySentException' 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): yiiwebResponse->sendHeaders()
    #1 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yiiwebResponse->send()
    #2 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yiiwebErrorHandler->renderException(Object(yiiwebHeadersAlreadySentException))
    #3 [internal function]: yiiaseErrorHandler->handleException(Object(yiiwebHeadersAlreadySentException))
    #4 {main}
    Previous exception:
    exception 'yiiwebHeadersAlreadySentException' 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): yiiwebResponse->sendHeaders()
    #1 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/base/Application.php(392): yiiwebResponse->send()
    #2 /home/mahdikas/public_html/guestbook/web/index.php(12): yiiaseApplication->run()
    #3 {main}

In the postController I have this code:

public function actionAdd_comment()
{
  //print_r($_POST);
  $model = new appmodelsComments;
  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!';
    }
  }
}

which line 117 in the error is:

echo 'Thanks for your comment.';

How can I solve this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since Yii 2.0.14 you cannot echo in a controller. A response must be returned:

public function actionAdd_comment() {
    $model = new appmodelsComments();
    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!';
        }
    }
}

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 appmodelsComments();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $this->someMagicWithEcho();
        exit;
    }
}

or

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...