Flash Data

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.


 public function actionFlashData() 
    { 
   $session = Yii::$app->session; 
   // set a flash message named as "welcome" 
   $session->setFlash('welcome', 'Successfully Logged In!'); 
   return $this->render('flashdata'); 
    } 

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


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

Step 3 Run it on the browser with the URL,


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