Yii Tutorial on Yii Using Flash Data

yii provides a concept of flash data. flash data is a session data which −

  • is set in one request.
  • will only be available on the next request.
  • will be automatically deleted afterwards.

step 1 − add an actionshowflash method to the sitecontroller.

public function actionshowflash() {
   $session = yii::$app->session;
   // set a flash message named as "greeting"
   $session->setflash('greeting', 'hello user!');
   return $this->render('showflash');
}

step 2 − inside the views/site folder, create a view file called showflash.php.

<?php
   use yii\bootstrap\alert;
   echo alert::widget([
      'options' => ['class' => 'alert-info'],
      'body' => yii::$app->session->getflash('greeting'),
   ]);
?>

step 3 − when you type http://localhost:8080/index.php?r=site/show-flash in the address bar of the web browser, you will see the following.

showflash php file

yii also provides the following session classes −

  • yii\web\cachesession − stores session information in a cache.

  • yii\web\dbsession − stores session information in a database.

  • yii\mongodb\session − stores session information in a mongodb.

  • yii\redis\session − stores session information using redis database.