Yii :: $ app-> session-> setFlash()不起作用

时间:2017-07-25 15:07:54

标签: yii2

SiteController中的以下代码适用于echo,但不适用于yii-method setflash()。也许,我必须重新配置我的配置文件(main-local.php)? 还有其他想法如何让setflash()完成它的工作吗?

   public function actionScript() { //A new method, programmed by Thomas Kipp
        $model = new myScriptForm();
        $fileName = 'file';
        $uploadPath = Yii::getAlias('@uploading');

        if (isset($_FILES[$fileName])) {
            $file = \yii\web\UploadedFile::getInstanceByName($fileName);

            if ($file->saveAs($uploadPath . '/' . $file->name)) {
                echo"<script>alert('Hallo');</script>";
                //echo \yii\helpers\Json::encode($file);
            }
        }
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->fileImage = UploadedFile::getInstance($model, 'fileImage');
            $model->avatar = UploadedFile::getInstances($model, 'avatar');
            if ($model->fileImage) {
                Yii::$app->session->setFlash('success', 'File has been uploaded');
                echo "<font size='4'><br><br><br><center>File <font color='red'> "
                . "$model->fileImage<font color='black'> successfully uploaded."
                . "<br>It's available in folder 'uploadedfiles' </font></font color></center>";
                $model->fileImage->saveAs(Yii::getAlias('@uploadedfilesdir/' . $model->fileImage->baseName . '.' . $model->fileImage->extension));
            } else {
                Yii::$app->session->setFlash('error', 'There has nothing to be uploaded');
                echo"<font size='4'><br><br><br><center>No Upload-file selected.<br>"
                . "Nothing moved into folder 'uploadedfiles'</font></center>";
            }
            if ($model->avatar) {
                $counter = 0;
                foreach ($model->avatar as $avatar) {
                    Yii::$app->session->setFlash('success', 'Avatar has been uploaded');
                    echo "<font size='4'><br><center>File <font color='red'> "
                    . "$avatar<font color='black'> successfully uploaded."
                    . "<br>It's available in folder 'uploadedfiles' </font></font color></center>";
                    $avatar->saveAs(Yii::getAlias('@uploadedfilesdir/' . $avatar->baseName . $counter . '.' . $avatar->extension));
                    $counter++;
                }
            } else {
                Yii::$app->session->setFlash('error', 'There has nothing to be uploaded');
                echo"<font size='4'><br><center>No Upload-file selected.<br>"
                . "Nothing moved into folder 'uploadedfiles' </font></center>";
            }
            return $this->render('myScript', ['model' => $model]);
        } else {
            return $this->render('myScript_Formular', ['model' => $model]);
        }
    }

1 个答案:

答案 0 :(得分:3)

一旦您使用例如:

指定了值
 Yii::$app->session->setFlash('error', 'There has nothing to be uploaded');

请确保在您的视图中例如:myScript.php 你正确地回应了设定的价值

<?= Yii::$app->session->getFlash('error'); ?>