Zend Framework 2 Flash Messenger 不返回任何消息

Zend Framework 2 Flash Messenger 不返回任何消息

问题描述:

我在 ZF2 中使用 Flash Messenger 时遇到了一个相当奇怪的问题.我在一个非常简单的场景中使用它,注册后保存注册完成"消息并重定向到登录页面并显示消息,但是 Flash Messenger 永远不会返回这些消息.

I'm having a rather odd problem with flash messenger in ZF2. I'm using it in quite a simple scenario, save a 'registration complete' message after registering and redirect to the login page and display the message however the messages are never returned by the flash messenger.

在控制器注册动作:

$this->flashMessenger()->addMessage('Registration complete');
return $this->redirect()->toRoute('default', array('controller' => 'user', 'action' => 'login'));

在控制器登录操作中:

$flashMessenger = $this->flashMessenger();
$mes = $flashMessenger->hasMessages();
$cur = $flashMessenger->hasCurrentMessages();

$mes 和 $cur 都是假的(为了确定我都试过).任何人都可以对此有所了解吗?

Both $mes and $cur are false (I tried both just to be sure). Can anyone shed any light on this?

我使用的是 ZF 2.2.2 和 PHP 5.3.14.会话保存处理程序正在使用 dbtable 适配器,我尝试禁用它,并将 flashmessenger 会话管理器设置为使用相同的 dbtable 保存处理程序,但没有结果.

I'm using ZF 2.2.2 and PHP 5.3.14. Session save handler is using the dbtable adapter and I have tried disabling this as well as setting the flashmessenger session manager to the use the same dbtable save handler with no result.

要使用 FlashMessenger 控制器插件,您需要在控制器中添加以下内容:

To use the FlashMessenger controller plugin, you need to add the following in your controller:

<?php 
class IndexController extends AbstractActionController {

  public function indexAction() {

    $this->flashMessenger()->addMessage('Your message');

    return $this->redirect()->toRoute('admin/default', array('controller'=>'index', 'action'=>'thankyou'));
  }

  public function thankyouAction() {

    return new ViewModel();
  }

}

将以下内容添加到 thankyou.phtml 视图模板:

Add the following to the thankyou.phtml view template:

<?php 

if ($this->flashMessenger()->hasMessages()) {

    echo '<div class="alert alert-info">';

    $messages = $this->flashMessenger()->getMessages();
    foreach($messages as $message) {
        echo $message;
    }

    echo '</div>';
}

?>