[moka同学笔记转载]Yii 设置 flash消息 创建一个渐隐形式的消息框

来源:http://www.cnblogs.com/xp796/p/5481004.html

Yii 设置 flash消息 创建一个渐隐形式的消息框

复制代码
 1 /*适用情况:比如提交一个表单,提交完成之后在页面展示一条提示消息。
 2 控制器里面这样写:
 3 单条消息:
 4 */
 5 \Yii::$app->getSession()->setFlash('error', 'This is the message');
 6 
 7 \Yii::$app->getSession()->setFlash('success', 'This is the message');
 8 
 9 \Yii::$app->getSession()->setFlash('info', 'This is the message');
10 #多条消息:
11 \Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']);
12 
13 #然后是视图里面:
14 
15 先引入Alert:use yii\bootstrap\Alert;
16 if( Yii::$app->getSession()->hasFlash('success') ) {
17     echo Alert::widget([
18         'options' => [
19             'class' => 'alert-success', //这里是提示框的class
20         ],
21         'body' => Yii::$app->getSession()->getFlash('success'), //消息体
22     ]);
23 }
24 if( Yii::$app->getSession()->hasFlash('error') ) {
25     echo Alert::widget([
26         'options' => [
27             'class' => 'alert-error',
28         ],
29         'body' => Yii::$app->getSession()->getFlash('error'),
30     ]);
31 }
复制代码

项目代码示例:

复制代码
 1 //c控制器里面这样写 CompanyInfoController 
 2 //公司信息
 3     public function actionIndex()
 4     {
 5         $result = CompanyService::CompanyInfo();
 6         $types = Yii::$app->params['companyType'];
 7         $model = CompanyInfo::find()->where(['id' =>Yii::$app->company->getId()])->one();
 8 
 9         if (Yii::$app->request->post() && CompanyService::UpdateConpanyInfo(Yii::$app->request->post())) {
10             Yii::$app->session->setFlash('flag', 'success');
11 
12             return $this->redirect('/system/company-info/index');
13         }
14         return $this->render('index', [
15             'staffNum' => $result['staffNum'],
16             'model' => $model,
17             'type' => $types,
18             'businessList' => $result['businessList'],
19             'businessParentId' => $result['businessParentId'],
20             'sonBusInessList' => $result['sonBusInessList']
21         ]);
22     }
23 
24 //视图里面 index.php
25 <script type="text/javascript">
26         //消息提示start
27         <?php $flag = Yii::$app->session->getFlash('flag');if($flag == 'success'): ?>
28 
29         layer.msg('公司信息更新成功');
30 
31         <?php endif; ?>
32         //消息提示end
33 
34        
35 </script>
复制代码
posted @ 2017-03-16 17:09  mokal同学  阅读(154)  评论(0编辑  收藏  举报