適用情況:比如提交一個表單,提交完成之後在頁面展示一條提示消息。
控制器裏面這樣寫:
單條消息:
\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語法寫?