Yii使用Flash數據

Yii提供閃存數據的概念。閃存數據是會話數據,其中 -
  • 在一個請求中設置
  • 只可在一個請求中
  • 隨後會被自動刪除
第1步 - 添加 actionShowFlash() 方法到 SiteController 中。 
public function actionShowFlash() {
   $session = Yii::$app->session;
   // set a flash message named as "greeting"
   $session->setFlash('greeting', 'Hello user!');
   return $this->render('showflash');
}
第2步 - 在檔夾 views/site 中,創建一個 showflash.php 的視圖檔。
<?php
   use yii\bootstrap\Alert;
   echo Alert::widget([
      'options' => ['class' => 'alert-info'],
      'body' => Yii::$app->session->getFlash('greeting'),
   ]);
?>
第3步 - 當你在Web流覽器的地址欄中輸入:http://localhost:8080/index.php?r=site/show-flash, 
你會看到以下內容。
Yii使用Flash數據
Yii 還提供了以下會話類 -
  • yii\web\CacheSession − 在緩存中存儲會話資訊

  • yii\web\DbSession − 在資料庫中存儲會話資訊

  • yii\mongodb\Session − 在MongoDB中存儲會話資訊

  • yii\redis\Session − 使用Redis資料庫存儲會話資訊



上一篇: Yii會話Sessions 下一篇: Yii Cookies