dyllen 2014-09-25 20:34:49 20328次浏览 11条回复 36 5 0

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

控制器里面这样写:

单条消息:

\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语法写?

觉得很赞
您需要登录后才可以回复。登录 | 立即注册