Yii2 Session, Flash 消息 [英] Yii2 Session, Flash messages

查看:35
本文介绍了Yii2 Session, Flash 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置 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());返回;}

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

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

<?php endif;?>

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

解决方案

要设置flash,try like

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

并显示它..

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

I have a problem with setting flash messages. So, i have an action which in some cases should redirect with flash. It looks like this:

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

After redirect in view i have this

<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>

The problem is i don't see any message here. In Debug panel i see SESSION var populated with good flash, but it doesn't display with this if-statement. Maybe i need to configure session component or something?...

解决方案

To set flash,try like

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

And to display it..

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

这篇关于Yii2 Session, Flash 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆