如何在 Yii2 中设置 flash 消息?

How to set a flash message in 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]);
    }

在视图中

 <div id="message">

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

现在我所做的结果不是我所期望的。我收到一条消息 "successfully got on to the payment page" 就像我已经回显了它一样。如果它类似于 echo 那么为什么我们需要 Yii2 中的 flash 消息。我想我的代码中可能遗漏了一些东西,使我的 flash 消息看起来像一个普通的消息。

正在设置即显消息

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

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

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

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
         ]);
    }
}

显示即显消息

我们使用 hasFlash() Method and to obtain the flash message we use the getFlash() 方法来检查即显消息。

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

所以在 view 中显示上面定义的 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; ?>

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

请参阅 guide 中有关即显消息的部分。

更少的代码。如果你不想要 if else 条件遵循

 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->getFlash('msg') ?>

下面是添加产品的控制器class

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
            ]);
        }
    }
}

我知道这是旧的,但它是 google 搜索中的第一个结果,所以我会更新它。 设置部分和yii2一样,你只需要在你的controller中添加即可

Yii::$app->session->setFlash('danger', 'you message');

setFlash 的第一个参数可以是 :

中的任何一个
error,danger,success,info,warning

这将决定 flash 消息的样式颜色。

现在对于显示部分,您所要做的就是将其放入您的布局文件中:

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

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

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