Thursday, 6 November 2014

Flash Message on view File in Zend2



FlashMessenger Documentation
Going by the documentation a simple use case for the flashMessenger would look like the following:
http://zf2.readthedocs.org/en/latest/modules/zend.mvc.plugins.html#the-flashmessenger

Step 1.
======================================================
Add Plugin on your Controller
======================================================
------------------
------------------
Use Zend\Mvc\Controller\Plugin\FlashMessenger <- Add this Plugin on your controller
------------------
------------------


Step 2.

In Controller, Your Code may be like this


public function commentAction()
{
    // ... display Form
    // ... validate the Form
    if ($form->isValid()) {
        // try-catch passing data to database
    --------------------------------------------------------------------------
    |    $this->flashMessenger()->addMessage('Thank you for your comment!'); |
    --------------------------------------------------------------------------
        return $this->redirect()->toRoute('blog-details'); //id, blabla
    }
}

public function detailsAction()
{
    // Grab the Blog with given ID
    // Grab all Comments for this blog
    // Assign the view Variables

    return array(
        'blog' => $blog,
        'comments' => $comments,
        -------------------------------------------------------------
        | 'flashMessages' => $this->flashMessenger()->getMessages() |
        -------------------------------------------------------------
    );
}


In Short, For Set the message Simply Call
$this->flashMessenger()->addMessage('Your Message Goes Here');

For Get the Message and set for view file, simply Call
$flashMessages = $this->flashMessenger()->getMessages();

Here $flashMessages is ready for to set the value for showing the message

No comments:

Post a Comment