"An error occurred while handling another error: yii\web\HeadersAlreadySentException"

Since Yii 2.0.14 you cannot echo in a controller. A 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!';
        }
    }
}

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;
    }
}

or

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();
    }
}

Although I accept rob006's solution as correct, I have encountered a situation where there was no echo in the controller, but I also got the error. After going through several sites looking for a solution, I discovered an alternative.

You can check the php.ini file and ensure the output buffer is enabled. If not, you can enable it by adding this line in php.ini if it does not exist:

output_buffering = on

And turn it off for just the script - the script where it is not required by either...

  1. calling ob_end_flush(), or
  2. calling ob_end_clean()

Tags:

Php

Yii

Yii2