【问题标题】:Send a message from controller to view从控制器发送消息以查看
【发布时间】:2015-06-03 05:28:14
【问题描述】:

我正在尝试将错误消息从我的控制器发送到我的视图。我尝试使用http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/,但我的Flash 消息没有到达。我是不是重定向不正确?

CrudController.php

// execution does reach here.
Yii::app()->user->setFlash('success', "Data saved!");
$this->redirect(array('crud/admin', 'id' => $currentUser->id));

Admin.php 也就是我的观点

<?php if(Yii::app()->user->hasFlash('success')):?>
    <div class="info">
        <?php echo Yii::app()->user->getFlash('success'); ?>
    </div>
<?php endif; ?>

那么如何向我的视图发送消息?我只是想向用户显示消息,老实说,我不在乎如何,除了条件检查发生在控制器而不是视图中。


var_dump(Yii::app()-&gt;user)之后

object(CWebUser)#21 (14) { ["allowAutoLogin"]=> bool(true) ["guestName"]=> 字符串(5) "Guest" ["loginUrl"]=> 数组(1) { [0]=> string(11) "/site/login" } ["identityCookie"]=> NULL ["authTimeout"]=> NULL ["autoRenewCookie"]=> bool(false) ["autoUpdateFlash"]=> bool(true) ["loginRequiredAjaxResponse"]=> NULL ["_keyPrefix":"CWebUser":private]=> 字符串(32) "a68200d8b7c100a1634ae9aa04a6e79e" ["_access":"CWebUser":private]=> 数组(0){}[“行为”]=>数组(0){} ["_initialized":"CApplicationComponent":private]=> bool(true) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL }

【问题讨论】:

  • 可能没有设置消息。在你的 Yii::app()->user 上做一个var_dump,看看里面有什么闪光信息。
  • @crowhill 我在帖子中添加了var_dump,但我不知道该信息的含义。你能看一下吗?
  • 在 Yii::app()->user->setFlash('success', "Data saved!");添加行: var_dump(Yii::app()->session->toArray());死();检查会话中是否存在您的消息。
  • @DanilaGanchar 什么都没有显示,因为它在控制器中。
  • @J_Strauton 这是不可能的。我的测试控制器和测试动作: Yii::app()->user->setFlash('success', "Data saved!"); var_dump(Yii::app()->session->toArray());死(); } 结果: Array([long_key] => 数据已保存![long_key2] => Array ( [success] => 0 ) )

标签: php model-view-controller yii


【解决方案1】:

我有同样的问题,但就我而言,我使用的是 TbAlert (YiiBooster)。问题是我的 PHP 版本低于 5.4 您想查看您的 PHP 版本吗?

【讨论】:

    猜你喜欢
    • 2019-03-18
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    相关资源
    最近更新 更多