【问题标题】:Yii2 advanced Fatal error Headers already sent inYii2 高级致命错误标头已发送
【发布时间】:2019-02-25 06:08:14
【问题描述】:

我正在使用 Yii2 Advanced。今天无需更改托管在虚拟主机上的我的网站上的任何内容。我在所有网络上的页面底部都收到了这条消息

Fatal error: Uncaught yii\web\HeadersAlreadySentException: Headers
already sent in
    /home/groopakc/public_html/vendor/yiisoft/yii2/web/Response.php on line 414.
    in /home/groopakc/public_html/vendor/yiisoft/yii2/web/Response.php:366
    Stack trace: #0 /home/groopakc/public_html/vendor/yiisoft/yii2/web/Response.php(339): 
    yii\web\Response->sendHeaders() #1 /home/groopakc/public_html/vendor/yiisoft/yii2/web/ErrorHandler.php(135):

    yii\web\Response->send() #2 /home/groopakc/public_html/vendor/yiisoft/yii2/base/ErrorHandler.php(262):

    yii\web\ErrorHandler->renderException(Object(yii\base\ErrorException))
#3 [internal function]: 
    yii\base\ErrorHandler->handleFatalError() #4 {main} 
    thrown in /home/groopakc/public_html/vendor/yiisoft/yii2/web/Response.php on line 366

我没有在我的控制器中使用echo,因为我确实读到了控制器内部的回显会导致此错误。但我做了检查,但我的所有控制器内都没有任何回声

我的控制器:SiteController.php 是

<?php

namespace frontend\controllers;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use frontend\models\Category;
use frontend\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\SignupForm;

/**
 * Site controller
 */

class SiteController extends Controller
{
    /***/
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /***/
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /***/
    public function actionIndex()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->redirect(['/dashboard']);
        }
        else {
            $categories = $this->findAllCategory();

            return $this->render('/category/viewIndex', [
                'categories' => $categories,
            ]);
        }
    }

    /***/
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->redirect(['/dashboard']);
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {

                return $this->redirect(['/dashboard']);

        } else {

            $model->password = '';

            return $this->renderAjax('/user/usrLog/login', [
                'model' => $model,
            ]);
        }
    }

    /***/
    public function actionSignup()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->redirect(['/dashboard']);
        }

        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    return $this->redirect(['/dashboard']);
                }
            }
        }

        return $this->renderAjax('/user/usrLog/signup', [
            'model' => $model,
        ]);
    }

    public function actionRequestPasswordReset()
    {
        $model = new PasswordResetRequestForm();
        $pswrd = hash('adler32', microtime().time());

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->userSavePass($model->userEmail()->email, $pswrd);

            Yii::$app->mailer->compose()
                ->setFrom('groopak@groopak.com')
                ->setTo($model->userEmail()->email)
                ->setSubject('New Password Request')
                ->setTextBody('Plain text content')
                ->setHtmlBody('<p>'.$model->userEmail()->username.'</p><p><b>Your request for new password</b></p><p>Your New Password is : '.$pswrd.'</p>')
                ->send();

            return $this->redirect(['index']);  
        }

        return $this->renderAjax('/user/usrLog/requestPasswordResetToken', [
            'model' => $model,
        ]);
    }

    /***/
    public function actionLogout()
    {
        if (Yii::$app->user->isGuest || Yii::$app->request->post('value') !== Yii::$app->user->identity->user_pid)
        {
            throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
        }
        elseif ( Yii::$app->request->post('value') == Yii::$app->user->identity->user_pid )
        {
            Yii::$app->user->logout();
            return $this->redirect(['index']);
        }
    }


    protected function findAllCategory()
    {
        return Category::find()->where(['category_statut' => 1])
                ->orderBy(['category_order' => SORT_ASC,])->all();
    }
}

我的 index.php 文件

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/../../vendor/yiisoft/yii2/Yii.php';
require __DIR__ . '/../../common/config/bootstrap.php';
require __DIR__ . '/../config/bootstrap.php';

$config = yii\helpers\ArrayHelper::merge(
    require __DIR__ . '/../../common/config/main.php',
    require __DIR__ . '/../../common/config/main-local.php',
    require __DIR__ . '/../config/main.php',
    require __DIR__ . '/../config/main-local.php'
);

(new yii\web\Application($config))->run();

我的布局文件 main.php

<?php

/* @var $this \yii\web\View */
/* @var $content string */

use yii\helpers\Html;
use yii\helpers\Url;
use frontend\assets\AppAsset;
use common\widgets\Alert;

use frontend\components\Style\StyleDetectorWidget;
use frontend\components\Style\StyleMediaWidget;
use frontend\components\MainLayout\LayoutHeaderWidget;
use frontend\components\MainLayout\LayoutSidebarWidget;


AppAsset::register($this);

?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="keywords" content="group,channel,media,share,photo,image,video,gallery,audio,film,social">
    <meta name = "viewport" content ="width=550">
    <link rel="icon" href="<?=Yii::getAlias('@web/gpkicon/favicon.ico')?>">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
    <?= StyleDetectorWidget::widget()?>
    <?= StyleMediaWidget::widget()?>
</head>

<body>
<?php $this->beginBody() ?>

    <!-- Wrapper -->
    <div id="wrapper">
        <div class="lyt-cc-sheet">
            <div class="lyt-cc-table">

                <!-- SIDEBAR -->
                <div id="sdbr" class="lyt-cc-cell lyt-cc-sidebar1">
                    <div id="sdbrSB" class="sbsdbr">
                        <?=LayoutSidebarWidget::widget()?>
                    </div>
                </div>

                <!-- CONTENT -->
                <div class="lyt-cc-cell lyt-cc-content">

                    <div class="header hdr-div" style="background:#eee">
                        <?=LayoutHeaderWidget::widget()?>
                    </div>

                    <div class="row main-row">
                        <?php use pa3py6aka\yii2\ModalAlert; ?>
                        <?= ModalAlert::widget() ?>
                        <?= $content ?>
                    </div>

                </div>

            </div>
        </div>
    </div>

<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

【问题讨论】:

  • 所以它出现在您网站上的所有controllersactions 上?或者只是SiteController
  • 在页面底部的所有页面上
  • 在所有控制器和操作上
  • 你能把网址显示一下吗?如果那里有echo,你有没有查看frontend/web/index.php?,你是通过composer update升级Yii版本的吗
  • hmm 这里没有什么奇怪的地方,尝试注释掉&lt;?= $content ?&gt; 部分以检查异常是否属于内容部分并缩小范围

标签: php yii2 yii2-advanced-app


【解决方案1】:

有时会发生这样的错误,当您在“?>”之后放置一些字符,例如空格或换行符。您需要删除它们并保存文件。它可能适用于本地主机,但不适用于真实主机。

?>
<-- you need remove new line
<-- you need remove new line

【讨论】:

    【解决方案2】:

    按照 Murat Kurbanov 的建议,我通过关闭文件末尾的 php 标签解决了我的问题。我只是放了一个?>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 2011-07-04
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 2013-10-09
      • 1970-01-01
      相关资源
      最近更新 更多