Zend Framework 2 Flash Messenger returning no messages

醉酒当歌 提交于 2019-12-04 07:50:41

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

?>

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.

Use

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

instead of

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!