“处理另一个错误时发生错误:yiiwebHeadersAlreadySentException";
我正在尝试针对基于 Yii 2 框架的留言簿应用程序提交评论.在我的 PC 上的本地主机上一切正常,但在共享主机上,当我想在 View 中提交评论时,出现此错误.
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.
这里是错误:
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}
在 postController 我有这个代码:
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!';
}
}
}
错误中的第 117 行是:
which line 117 in the error is:
echo 'Thanks for your comment.';
我该如何解决这个问题?
How can I solve this problem?
推荐答案
从 Yii 2.0.14 开始,你不能在控制器中回显.必须返回响应:
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!';
}
}
}
您也可以在方法的末尾调用 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 appmodelsComments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$this->someMagicWithEcho();
exit;
}
}
或
public function actionAdd_comment() {
$model = new appmodelsComments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
ob_start();
$this->someMagicWithEcho();
return ob_get_clean();
}
}
相关文章