Zend Framework 2 Flash Messenger returning no mess

2020-07-14 05:58发布

问题:

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.

回答1:

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

?>


回答2:

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.



回答3:

Use

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

instead of

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


回答4:

I also faced same problem for (login flashmessage after registration) I solved it in the following way

Apply a check on your layout page like

<?php if($this->zfcUserIdentity()) {  ?> 
                        <div id="flashMessageDiv" class="hide">
                            <?php echo isset($flashMessages) && isset($flashMessages['0']) ? $flashMessages['0'] : ''; ?>
                        </div>
                    <?php } ?>

That means layout flashMessageDiv is accessible only to the logined user . now on your login view file (login.phtml) apply the following code

   <?php $pathArray = $_SERVER['HTTP_REFERER'];
            $pathArray = explode("/",$pathArray);
        ?>

       <?php if ($pathArray[4] === 'register') { ?>   
            <div id="flashMessageDiv" class="hide">
                <?php  echo "User details saved successfully"; ?>
            </div>
       <?php } ?>

In the above code i used HTTP_REFERER which will simply give us the referer url details check if referer url is register then show falshmessage.

Hope it will help you.



回答5:

The FlashMessenger is now an official view helper in ZF2 and can be easily integrated in every view / layout: FlashMessenger Helper — Zend Framework 2 2.3.1 documentation - Zend Framework

It works with TwitterBootstrap3 too and there is an alternative configuration for your module.config.php.