yii2.0 - Yii::$app->getSession()->setFlash

适用情况:比如提交一个表单,提交完成之后在页面展示一条提示消息。

控制器里面这样写:
单条消息:
1 Yii::$app->getSession()->setFlash('error','文件格式错误',true);
2 Yii::$app->getSession()->setFlash('error', 'This is the message'); // 失败
3 Yii::$app->getSession()->setFlash('success', 'This is the message'); // 成功
4 Yii::$app->getSession()->setFlash('info', 'This is the message'); // 提示后的信息不回消失,要手动
5 return $this->render(['index']);
单条消息

 

多条消息:

1 Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']); // 多条消息
2 return $this->render(['index']);
多条消息

 

其他:

Yii::$app->session->setFlash('flag','success');
$b = Yii::$app->session->getFlash('flag','success');
var_dump($b); // success

#返回到上次访问的地址,且输出提示
Yii::$app->getSession()->setFlash('success',"恭喜你,操作成功!",true);
return $this->redirect(Yii::$app->request->referrer);

 

posted @ 2018-04-26 18:57  liuweipcs  阅读(450)  评论(0)    收藏  举报