使用flash消息

当你使用一个form编辑一个模型时,删除一个模型,或者做其它操作,这是一个好习惯,告诉用户是否工作正常,或者发生了错误。典型情况下,在一些动作之后,例如编辑一个form,一个重定向将会发生,并且我们需要在页面上展示一条信息。但是,我们应该如何将它从当前页面传递到重定向目标,然后清理干净?flash消息将会帮助我们。

准备

按照官方指南http://www.yiiframework.com/doc-2.0/guide-start-installation.html的描述,使用Composer包管理器创建一个新的应用。

如何做…

  1. 创建一个控制器@app/controllers/TestController.php
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. use yii\filters\AccessControl;
  6. class TestController extends Controller
  7. {
  8. public function behaviors()
  9. {
  10. return [
  11. 'access' => [
  12. 'class' => AccessControl::className(),
  13. 'rules' => [
  14. [
  15. 'allow' => true,
  16. 'roles' => ['@'],
  17. 'actions' => ['user']
  18. ],
  19. [
  20. 'allow' => true,
  21. 'roles' => ['?'],
  22. 'actions' => ['index', 'success',
  23. 'error']
  24. ],
  25. ],
  26. 'denyCallback' => function ($rule, $action) {
  27. Yii::$app->session->setFlash('error',
  28. 'This section is only for registered users.');
  29. $this->redirect(['index']);
  30. },
  31. ],
  32. ];
  33. }
  34. public function actionUser()
  35. {
  36. return $this->renderContent('user');
  37. }
  38. public function actionSuccess()
  39. {
  40. Yii::$app->session->setFlash('success', 'Everything went fine!');
  41. $this->redirect(['index']);
  42. }
  43. public function actionError()
  44. {
  45. Yii::$app->session->setFlash('error', 'Everything went wrong!');
  46. $this->redirect(['index']);
  47. }
  48. public function actionIndex()
  49. {
  50. return $this->render('index');
  51. }
  52. }
  1. 此外,创建@app/views/common/alert.php视图:
  1. <?php
  2. use yii\bootstrap\Alert;
  3. ?>
  4. <?php if (Yii::$app->session->hasFlash('success')):?>
  5. <?= Alert::widget([
  6. 'options' => ['class' => 'alert-success'],
  7. 'body' => Yii::$app->session->getFlash('success'),
  8. ]);?>
  9. <?php endif ?>
  10. <?php if (Yii::$app->session->hasFlash('error')) :?>
  11. <?= Alert::widget([
  12. 'options' => ['class' => 'alert-danger'],
  13. 'body' => Yii::$app->session->getFlash('error'),
  14. ]);?>
  15. <?php endif; ?>
  1. 创建视图@app/views/test/index.php
  1. <?php
  2. /* @var $this yii\web\View */
  3. ?>
  4. <?= $this->render('//common/alert') ?>
  5. <h2>Guest page</h2>
  6. <p>There's a content of guest page</p>
  1. 创建视图@app/views/test/user.php
  1. <?php
  2. /* @var $this yii\web\View */
  3. ?>
  4. <?= $this->render('//common/alert') ?>
  5. <h2>User page</h2>
  6. <p>There's a content of user page</p>
  1. 现在,如果你访问http://yii-book.app/index.php?r=test/success,你将会被重定向到http://yii-book.app/index.php?r=test/index,并展示了一条成功的消息:

使用flash消息 - 图1

  1. 此外,如果你访问http://yii-book.app/index.php?r=test/error,你将会被重重定向到相同的页面上,但是会得到一条错误消息。刷新index页面,消息将会隐藏:

使用flash消息 - 图2

  1. 尝试运行http://yii-book.app/index.php?r=test/user。你将会被重定向到http://yii-book.app/index.php?r=test/index,并会在denyCallback函数中执行并展示一条错误消息:

使用flash消息 - 图3

工作原理…

我们使用Yii::$app->session->('success', 'Everything went fine!')设置一条flash消息。本质上,它保存了一条消息到一个session中,所以在最低等级上,我们的消息被保存在$_SESSION中,直到Yii::$app->session->getFlash('success')被调用,然后$_SESSION键会被删除。

这个flash消息将会在请求中访问之后被自动删除。

更多…

getAllFlashes()方法

有时你需要处理所有的flashs。你可以使用一个简单的方式来处理它,如下所示:

  1. $flashes = Yii::$app->session->getAllFlashes();
  2. <?php foreach ($flashes as $key => $message): ?>
  3. <?= Alert::widget([
  4. 'options' => ['class' => 'alert-info'],
  5. 'body' => $message,
  6. ]);
  7. ?>
  8. <?php endforeach; ?>

removeAllFlashes()方法

当你需要flush所有的flash时,使用下面的方法:

  1. Yii::$app->session->removeAllFlashes();

removeFlash()方法

当你需要移除指定的键,使用如下方法:

  1. Yii::$app->session->removeFlash('success');

在这个例子中,我们添加一个非常有用的回调函数,它设置一条错误信息,并重定向到test/index页面上。

参考

欲了解更多信息,参考: