2017-07-25 70 views
1

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

一旦你擁有了分配與例如值:

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

確保在您看來例如:myScript.php 您正確呼應設置好的

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

THX了很多的價值。這解決了我的問題! – tklustig

+0

@tklustig好,如果我的答案是正確的,請將其標記爲已接受...看看這裏如何 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – scaisEdge