Message zend Framework flash messanger and redirection

So, I am creating a project using zend-framwork, and I am trying to implement a flash messenger assistant, but I can not find good practice to implement it. I need to use a flash messenger to send a message and redirect while the message appears directly at a specific location in layout.phtml. I know that for a redirector I can do this:

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

What can I do for a flash messenger to make it work? and for what is best?

+3
source share
3 answers

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 when you want to use it

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

You will be redirected almost every time after using flashMessenger.

+6

- Zend , 'foo'

public function fooAction(){

 $flashMessenger = $this->_helper->getHelper('FlashMessenger');
 //some codes
 $flashMessenger->addMessage(array('error' => 'This is an error message'));
$this->_redirect('/someothercontroller/bar');

}
//someothercontroller/barAction
public function barAction(){

$flashMessenger = $this->_helper->getHelper('FlashMessenger');
 $this->view->flashmsgs = $flashMessenger->getMessages();  //pass it to view 

}

<?php if(isset($this->flashmsgs)) { ?>
                 <?php foreach($this->flashmsgs as $msg){ 
                       foreach ($msg as $key=>$diserrors) {
                        if($key=="error"){?>
      //do wat you want with your message
<?php } } }?>
+2

/**
 * @var Zend_Controller_Action_Helper_FlashMessenger
 */
protected $flashMessenger = null;


/**
 * initalize flash messenger
 *
 * @return void
 */
public function init() 
{
    $this->flashMessenger = $this->_helper->FlashMessenger;
}

/**
 * Action wich is redirectin.. and sets message
 *
 * @return void
 */
public function actiononeAction()
{
    $this->flashMessenger->addMessage('FLY HiGH BiRD!');
    $this->_redirect('/whatever/url/in/your/project');
}

/**
 * display messages from messenger
 * 
 * @return void
 */
public function displayAction()
{
    $myMessages = $this->flashMessenger->getMessages();
    $this->view->messages = $myMessages;
}
0

All Articles