zend framework flash messanger message and redirect

dori naji picture dori naji · May 7, 2012 · Viewed 11.1k times · Source

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 ?

Answer

Mr Coder picture Mr Coder · May 7, 2012

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.