如何在Yii2中设置flash消息?

如何在Yii2中设置flash消息?,yii2,flash-message,Yii2,Flash Message,我遵循了这一点。我的代码如下 内部控制器 public function actionFunction4() { $this->layout="sintel"; $model= new Customers(); \Yii::$app->getSession()->setFlash('success', 'successfully got on to the payment page'); return $t

我遵循了这一点。我的代码如下 内部控制器

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]);
    }
Yii::$app->session->setFlash('danger', 'you message');
在视图中

 <div id="message">

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


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

设置闪光信息

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

可以使用以下方法设置Flash消息

控制器
文件中添加以下代码,如下所示:

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

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
         ]);
    }
}
显示闪光信息

为了检查flash消息,我们使用该方法,为了获得flash消息,我们使用该方法

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

因此,在
视图中显示上面定义的闪存消息是由

// 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; ?>
//显示成功消息
×
保存的!
//显示错误消息
×
保存的!

flash消息的优点是它只显示一次。您不再需要提供if/else逻辑。如果将显示flash消息的代码放在布局视图文件(通常是view/layout/main.php)中,则可以在需要的每个操作中设置flash消息,使用正常响应或重定向,并且可以确保只显示一次。这让生活变得容易了一点。这就是flash消息的想法——并不是说它会在一段时间后消失


请参阅.Less代码中有关flash消息的部分。如果你不想,如果其他条件遵循

 Yii::$app->session->setFlash('msg', '
     <div class="alert alert-success alert-dismissable">
     <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
     <strong>Validation error! </strong> Your message goes here.</div>'
  );
Yii::$app->session->setFlash('msg','
×
验证错误!您的消息显示在此处。”
);
在你看来

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

下面是用于添加产品的控制器类

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

        if ($ProductsModel->load(Yii::$app->request->post()) && $ProductsModel->save()) {
            Yii::$app->session->setFlash('success', "Product Added Successfully");
            return $this->redirect(['create']);
        }
        else{ 
            return $this->render('create', [
                'ProductsModel' => $ProductsModel
            ]);
        }
    }
}

我知道这是旧的,但这是谷歌搜索的第一个结果,所以我会更新它。 yii2中的设置部分仍然相同,只需将其添加到控制器中即可

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]);
    }
Yii::$app->session->setFlash('danger', 'you message');
setFlash
的第一个参数可以是:

error,danger,success,info,warning
这将决定flash消息的样式颜色

现在,对于显示零件,只需将其放置在布局文件中:

<?= common\widgets\Alert::widget() ?>

如果没有布局文件,只需将其添加到要在其中显示falsh消息的任何视图中即可


希望这个答案对其他人有帮助。

谢谢,它起作用了。所以我现在有两种方法:——)回答得好。非常感谢。更多地解释了Yii2 Flash消息。我认为这里有一个小错误
应该是
只是引导类的更改!谢谢我对flash消息的概念是完全不同的。你对flash消息是什么(用yii)感到困惑。它只是一条信息,只显示一次,然后从会话中删除。是的,我有点困惑。