Flash data is a kind of session data, which possesses the following features.

  • Set in one request.
  • Only available during the next request.
  • Automatically deleted afterwards.

It is mainly used to deliver messages to the end users that is delivered only once such as confirmation messages sent after the login.

Example

Step 1 Create an action ationFlashData in the SiteController.php file.

  1. public function actionFlashData()   
  2.    {   
  3.   $session = Yii::$app->session;   
  4.   // set a flash message named as "welcome"   
  5.   $session->setFlash('welcome''Successfully Logged In!');   
  6.   return $this->render('flashdata');   
  7.    }   

Step 2 Create a view file flashdata.php in the views/site folder.

  1. <?php   
  2.    use yii\bootstrap\Alert;   
  3.    echo Alert::widget([   
  4.       'options' => ['class' => 'alert-info'],   
  5.       'body' => Yii::$app->session->getFlash('welcome'),   
  6.    ]);   
  7. ?>  

Step 3 Run it on the browser with the URL,

  1. http://localhost/flash/frontend/web/index.php?r=site/flash-data  
YII Flash data 1