Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 Flash Messenger returning no messages

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.

like image 425
JaiCee Avatar asked Feb 04 '14 10:02

JaiCee


3 Answers

It seems that your code is as it should be, there must be something tricky in the workflow.

In this case, you can debug the old way : try var_dump($_SESSION) to see if it is populated by your flashMessenger.

like image 28
jmleroux Avatar answered Oct 23 '22 11:10

jmleroux


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

?>
like image 75
OlegKrivtsov Avatar answered Oct 23 '22 12:10

OlegKrivtsov


Use

echo $this->flashMessenger()->renderCurrent(...

instead of

echo $this->flashMessenger()->render(...
like image 31
Mark Avatar answered Oct 23 '22 11:10

Mark