zend framework flash messanger message and redirect

11,223

Solution 1

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.

Solution 2

How to use flash messenger in Zend suppose you have an action called '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 

}

In your view part

<?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 } } }?>
Share:
11,223
dori naji
Author by

dori naji

Updated on June 04, 2022

Comments

  • dori naji
    dori naji almost 2 years

    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 ?

  • shrikeh
    shrikeh almost 12 years
    Controllers should not really keep state; therefore, creating variables for them is not good practice. Instead, simply use $this->_helper->flashMessenger, or if bored of typing longhand, assign it per action as $flashMessenger = $this->_helper->flashMessenger. Use the request object for state in a request if you aren't using a model.
  • coolguy
    coolguy almost 12 years
    This will only gets one flash message at a time..you cannot iterate
  • Mr Coder
    Mr Coder almost 12 years
    I have never encounter any need of more then one message .
  • dori naji
    dori naji almost 12 years
    this worked fine for now, But i have one question why the auto complete in net beans doesn't show the auto complete when i am using the _helper and how can i make to do so ??
  • Mr Coder
    Mr Coder almost 12 years
    @dorinaji Since helper uses php magic function __call hence its not possible to determine the name of helper .But Zend Studio (IDE) do show auto complete for helpers since its aware of ZF structure . Its one of the important features for which I use Zend Studio myself.