Yii2.0页面提示消息

来源:互联网 发布:未来网络实验室 编辑:程序博客网 时间:2024/05/22 07:54

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

控制器里面这样写:

单条消息:

\Yii::$app->getSession()->setFlash('error', 'This is the message');

\Yii::$app->getSession()->setFlash('success', 'This is the message');

\Yii::$app->getSession()->setFlash('info', 'This is the message');

多条消息:

\Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']);

然后是视图里面:

先引入Alert:use yii\bootstrap\Alert;

然后是:

if( Yii::$app->getSession()->hasFlash('success') ) {echo Alert::widget(['options' => ['class' => 'alert-success', //这里是提示框的class],'body' => Yii::$app->getSession()->getFlash('success'), //消息体]);}if( Yii::$app->getSession()->hasFlash('error') ) {echo Alert::widget(['options' => ['class' => 'alert-error',],'body' => Yii::$app->getSession()->getFlash('error'),]);}

如果有消息就会显示对应消息,表现是一个div,和bootstrap的警告框是一样的。
你想把消息提示放在哪里,把上述代码就放到那里就可以了。

*** 题外话,这个编辑器是要用Markdown语法写?



0 0
原创粉丝点击