Zend Framework 2 Flash Messenger returning no messages

flashmessenger, php, zend-framework, zend-framework2

Solution

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

}

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>';
}

?>

Problem

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. In controller register action: ``` $this->flashMessenger()->addMessage('Registration complete'); return $this->redirect()->toRoute('default', array('controller' => 'user', 'action' => 'login')); ``` In controller login action: ``` $flashMessenger = $this->flashMessenger(); $mes = $flashMessenger->hasMessages(); $cur = $flashMessenger->hasCurrentMessages(); ``` Both $mes and $cur are false (I tried both just to be sure). Can anyone shed any light on this? 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.

Original source