3

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 ?

dori naji
  • 980
  • 1
  • 16
  • 41

3 Answers3

6

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.

Mr Coder
  • 8,169
  • 5
  • 45
  • 74
  • This will only gets one flash message at a time..you cannot iterate – coolguy May 07 '12 at 11:08
  • I have never encounter any need of more then one message . – Mr Coder May 07 '12 at 11:25
  • 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 ?? – dori naji May 07 '12 at 21:15
  • @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. – Mr Coder May 08 '12 at 04:19
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 } } }?>
coolguy
  • 7,866
  • 9
  • 45
  • 71
0

This should work inside an Controller

/**
 * @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;
}
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • 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. – shrikeh May 07 '12 at 10:28