如何在 Yii2 中设置闪现消息? [英] How to set a flash message in Yii2?

查看:48
本文介绍了如何在 Yii2 中设置闪现消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了这个链接.我的代码如下在控制器中

公共函数 actionFunction4(){$this->layout="sintel";$model=新客户();\Yii::$app->getSession()->setFlash('success', '成功进入支付页面');return $this->render("function4",['model'=>$model]);}

在视图中

 

<?= Yii::$app->session->getFlash('success');?>

现在我所做的结果不是我所期望的.我收到一条消息成功进入付款页面",就像我已经回显一样.如果它类似于 echo 那么为什么我们需要在 Yii2 中使用 flash 消息.我想我可能在我的代码中遗漏了一些让我的 flash 消息看起来像普通消息的东西.

解决方案

设置闪信

快速消息用于通过同一用户的一个或多个请求将消息保持在会话中.默认情况下,它会在显示给用户后从会话中删除.

Flash 消息可以使用 setFlash 设置() 方法

在您的 controller 文件中添加以下代码,例如:

Yii::$app->session->setFlash('success', "您要显示的消息.");

示例:

class ProductsController 扩展 \yii\web\Controller{公共函数 actionCreate(){$model = 新用户();if ($model->load(Yii::$app->request->post())) {如果 ($model->save()) {Yii::$app->session->setFlash('success', "用户创建成功.");} 别的 {Yii::$app->session->setFlash('error', "用户未保存.");}返回 $this->redirect(['index']);}返回 $this->render('create', ['模型' =>$model]);}}

显示快讯

为了检查 Flash 消息,我们使用 hasFlash() 方法并获取 flash 消息,我们使用 getFlash() 方法.

默认情况下,获取消息会将其从会话中删除.这意味着消息仅显示在提供给用户的第一页上.获取方法有一个布尔参数可以改变这种行为.

因此在 view 中显示上面定义的 flash 消息是由

完成的

//显示成功信息<?php if (Yii::$app->session->hasFlash('success')): ?><div class="alert alert-success alert-dismissable"><button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button><h4><i class="icon fa fa-check"></i>已保存!</h4><?= Yii::$app->session->getFlash('success') ?>

<?php endif;?>//显示错误信息<?php if (Yii::$app->session->hasFlash('error')): ?><div class="alert alert-danger alert-dismissable"><button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button><h4><i class="icon fa fa-check"></i>已保存!</h4><?= Yii::$app->session->getFlash('error') ?>

<?php endif;?>

i followed this Link. My code is as follows in controller

public function actionFunction4()
    {
        $this->layout="sintel";
        $model= new Customers();
        \Yii::$app->getSession()->setFlash('success', 'successfully got on to the payment page');
        return $this->render("function4",['model'=>$model]);
    }

in the view

 <div id="message">

          <?= Yii::$app->session->getFlash('success');?>
      </div>

now the result of what i did is not what i expected. I got a message "successfully got on to the payment page" like i have echo ed it. If it is similar to echo then why do we need a flash message in Yii2. I think i may be missing something in my code that make my flash message appear like a regular one.

解决方案

Setting flash message

A flash message is used in order to keep a message in session through one or several requests of the same user. By default, it is removed from session after it has been displayed to the user.

Flash messages can be set using the setFlash() Method

Add below code in your controller file like:

Yii::$app->session->setFlash('success', "Your message to display.");

Example:

class ProductsController extends \yii\web\Controller
{
    public function actionCreate()
    {
         $model = new User();

         if ($model->load(Yii::$app->request->post())) {
              if ($model->save()) {
                  Yii::$app->session->setFlash('success', "User created successfully."); 
              } else {
                  Yii::$app->session->setFlash('error', "User not saved.");
              }
              return $this->redirect(['index']);
         }
         return $this->render('create', [
             'model' => $model
         ]);
    }
}

Displaying flash message

To check for flash messages we use the hasFlash() Method and to obtain the flash message we use the getFlash() Method.

By default, fetching a message deletes it from the session. This means that a message is meant to be displayed only on the first page served to the user. The fetching methods have a boolean parameter that can change this behavior.

So showing of the flash message defined above in a view is done by

// display success message
<?php if (Yii::$app->session->hasFlash('success')): ?>
    <div class="alert alert-success alert-dismissable">
         <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
         <h4><i class="icon fa fa-check"></i>Saved!</h4>
         <?= Yii::$app->session->getFlash('success') ?>
    </div>
<?php endif; ?>

// display error message
<?php if (Yii::$app->session->hasFlash('error')): ?>
    <div class="alert alert-danger alert-dismissable">
         <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
         <h4><i class="icon fa fa-check"></i>Saved!</h4>
         <?= Yii::$app->session->getFlash('error') ?>
    </div>
<?php endif; ?>

这篇关于如何在 Yii2 中设置闪现消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆