Search code examples
zend-frameworkactionhelperflash-message

zend framework flash messanger message and redirect


So i am creating a project using zend-framwork and i am trying to implement the flash messenger helper but i cant find any good practice to implement it. What i need is to use the flash messenger to send a message and redirect, while the message will appear directly in a specific place in the layout.phtml. I know that for the redirector i can do that:

$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$redirector->gotoUrl('/my-controller/my-action/param1/test/param2/test2')
               ->redirectAndExit();'

What can i do to the flash messanger to make it work ? and what is the best practice for that ?


Solution

  • In your controller

    public function init()
    {
    $messages = $this->_helper->flashMessenger->getMessages();
    if(!empty($messages))
    $this->_helper->layout->getView()->message = $messages[0];
    }
    

    in your layout.phtml

        <!-- Global notification handling to use call flashMessenger action helper -->
        <?php if(isset($this->message)) :?>
        <div class="notification">
    
        <?php echo $this->message ;?>
    
        </div>    
     <?php endif;?>
    

    Then whenever you want to use it

    public function loginAction()
    {
    $this->_helper->flashMessenger('Login is success');
    $this->_helper->redirector('home');
    }
    

    Almost every-time you will be redirecting after using flashMessenger.