yii2 学习笔记 ---- Yii2页面提示消息
单条消息:
\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']);
实例:
控制器:
Yii::$app->getSession()->setFlash('error', '跳转方式为跳转到网页(跳转链接或者是名称,根据link_type决定)只能填网址');
试图:
use yii\bootstrap\Alert;
<?php
if( Yii::$app->getSession()->hasFlash('error') ) {
//如果想和js一样弹窗就用下面注释的代码
//echo "<script>alert('" . Yii::$app->session->getFlash('error') . "')</script>";
echo Alert::widget([
'options' => [
'class' => 'alert-error',
],
'body' => Yii::$app->getSession()->getFlash('error'),
]);
} ?>
效果图: