【问题标题】:Yii2 Session, Flash messagesYii2 会话,Flash 消息
【发布时间】:2014-10-11 14:22:37
【问题描述】:

我在设置 Flash 消息时遇到问题。 所以,我有一个动作,在某些情况下应该用 flash 重定向。它看起来像这样:

if(!$this->_isSameOrg($reports)){
    \Yii::$app->session->setFlash('consol_v_error',\Yii::t('app/consol', 'some_text'));
    $this->redirect(\Yii::$app->request->getReferrer());
    return;
}

在视图中重定向后我有这个

<div class="col-lg-12">
    <?php if(Yii::$app->session->hasFlash('consol_v_error')): ?>
        <div class="alert alert-danger" role="alert">
            <?= Yii::$app->session->getFlash('consol_v_error') ?>
        </div>
    <?php endif; ?>
</div>

问题是我在这里没有看到任何消息。在调试面板中,我看到 SESSION var 填充了良好的闪存,但它没有显示这个 if 语句。 也许我需要配置会话组件或其他东西?...

【问题讨论】:

  • 这可能与重定向有关,尝试渲染一个测试视图而不是 $this-&gt;redirect(\Yii::$app-&gt;request-&gt;getReferrer()); 并在那里显示 flash 消息。
  • 你使用高级模板吗?

标签: session yii2


【解决方案1】:

要设置闪光灯,请尝试

  \Yii::$app->getSession()->setFlash('error', 'Your Text Here..');
   return $this->redirect('Your Action');

并显示它..

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

【讨论】:

  • 那是因为返回的 flash 在一个数组中。你可能遇到了如下错误: PHP Notice – yii\base\ErrorException Array to string conversion
【解决方案2】:

你可以这样试试

<?php
foreach (Yii::$app->session->getAllFlashes() as $key => $message) {
echo '<div class="alert alert-' . $key . '">' . $message . '</div>';
}
?>

【讨论】:

  • 使用全局 Flash 部分(例如在布局中)呈现任何 Flash 消息的非常好的解决方案。 +1
  • 这不是一个好的解决方案,因为它会检索应用程序的所有会话消息。如果您想在站点的单个部分中实现,那么最好的方法是使用 = Yii::$app->session->getFlash('error'); ?>
【解决方案3】:

简单地做:

  1. 将两个字符串添加到:/views/layout/main.php

    • 在区块use:

    use frontend\widgets\Alert;
    
    • 之前&lt;?= $content ?&gt;:

    <?= Alert::widget() ?>
    
  2. 现在所有消息都会自动显示在屏幕上。让我们试试吧!添加任意控制器的方法:
Yii::$app->session->setFlash('warning', 'bla bla bla bla 1');

Yii::$app->session->setFlash('success', 'bla bla 2');

Yii::$app->session->setFlash('error', 'bla bla 3');

【讨论】:

【解决方案4】:

而不是这个:

$this->redirect(\Yii::$app->request->getReferrer());

return;

试试这个:

return $this->redirect(\Yii::$app->request->getReferrer());

对我来说效果很好。

【讨论】:

  • 你能详细说明为什么这是有效的吗? (我们为什么要返回值?)。谢谢!
【解决方案5】:

在yii2 flash中可以这样设置

Yii::$app->session->setFlash('success', 'Thank you ');

【讨论】:

    【解决方案6】:

    这是我的解决方案: 覆盖标准会话类:

    namespace app\components;
    
    use Yii;
    
    class Session extends \yii\web\Session {
    
        public function getAllFlashesNormalized() {
            $flashes = [];
            foreach (Yii::$app->session->getAllFlashes() as $key => $flash) {
                if (is_array($flash))
                    foreach ($flash AS $message)
                        $flashes[] = ['key' => $key, 'message' => $message];
                else
                    $flashes[] = ['key' => $key, 'message' => $flash];
            }
    
            return $flashes;
        }
    }
    

    所以你可以:

    Yii::$app->session->addFlash('success', 'Text.');
    Yii::$app->session->addFlash('success', 'Another text.');
    

    并输出此消息:

    <?php foreach (Yii::$app->session->getAllFlashesNormalized() as $flash) { ?>
        <div class="alert alert-<?=$flash['key']?>" role="alert"><?=$flash['message']?></div>
    <?php } ?>
    

    【讨论】:

      【解决方案7】:

      在我的情况下,当我在重定向之前使用 hasFlash 时,重定向后删除了 flash 消息。

      if (!Yii::$app->getSession()->hasFlash('success')) {
          Yii::$app->getSession()->setFlash('success', Yii::t('app', 'your text'));
      }  
      

      所以我添加了这个,它有帮助

      if (!Yii::$app->getSession()->hasFlash('success')) {
          Yii::$app->getSession()->setFlash('success', Yii::t('app', 'your text'));
      } else {
          Yii::$app->getSession()->set('__flash', array('success' => -1));
      } 
      

      【讨论】:

        【解决方案8】:

        对我不起作用。 我宁愿使用:

        在控制器中:

        $session = new Session;
        $session->addFlash("warning","Your text here");
        

        在视图中:

        <?php 
        $session = new Session;
        foreach ($session->getAllFlashesNormalized() as $flash) { 
        ?>
        <div class="alert alert-<?=$flash['key']?>" role="alert">
            <?=$flash['message']?>
        </div>
        <?php 
        } 
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-26
          • 1970-01-01
          • 1970-01-01
          • 2021-01-01
          • 1970-01-01
          • 2023-04-03
          相关资源
          最近更新 更多