Extended Zend FlashMessenger
The default Flash Messenger of the Zend Framework 1.x is not able to group different types of messages. If you want to handle error, information or success messages and display them in
different styles (e.g. green div for success, red div for alert etc.) you can now use the extended FlashMessenger. You can also add more than one message of different types.
Or simply use composer ;)
<?php
# application/controllers/SampleController.php
class SampleController extends Zend_Controller_Action {
public function indexAction() {
# Adds a information message
$this->_helper->flashMessenger->addInformationMessage( 'I am a information message!' );
# Adds success messages
$this->_helper->flashMessenger->addSuccessMessage( 'I am a success message!' );
$this->_helper->flashMessenger->addSuccessMessage( 'I am a second success message!' );
# Adds a error message
$this->_helper->flashMessenger->addErrorMessage( 'I am a error message!' );
}
}
?>
# application/views/scripts/sample/index.phtml
# To display the messages in your view, just call the printMessage() - View Helper
<div class="container">
<?php echo $this->printMessage();
</div>
# Output:
<div class="container">
<div class="alert alert-info">
<div>I am a information message!</div>
</div>
<div class="alert alert-success">
<div>I am a success message!</div>
<div>I am a second success message!</div>
</div>
<div class="alert alert-error">
<div>I am a error message!</div>
</div>
</div>
It’s also possible to group the messages. It’s useful if you want to have multiple message outputs on your page.
If you want to group the messages, just use the second param of the “add(Information|Success|Error)Message-Methods to define the name of the group (see example).
To display messages of a group, just pass the group name into the printMessage View Helper (see example).
<?php
# application/controllers/SampleController.php
class SampleController extends Zend_Controller_Action {
public function indexAction() {
# Adds a information message
$this->_helper->flashMessenger->addInformationMessage( 'I am a information message!', 'GROUPA' );
# Adds success messages
$this->_helper->flashMessenger->addSuccessMessage( 'I am a success message!', 'GROUPA' );
$this->_helper->flashMessenger->addSuccessMessage( 'I am a second success message!', 'GROUPB' );
# Adds a error message
$this->_helper->flashMessenger->addErrorMessage( 'I am a error message!', 'GROUPB' );
}
}
?>
# application/views/scripts/sample/index.phtml
# To display the messages in your view, just call the printMessage() - View Helper
<div class="container">
<div class="span6">
<?php echo $this->printMessage( 'GROUPA' );
</div>
<div class="span6">
<?php echo $this->printMessage( 'GROUPB' );
</div>
</div>
# Output:
<div class="container">
<div class="span6">
<div class="alert alert-info">
<div>I am a information message!</div>
</div>
<div class="alert alert-success">
<div>I am a success message!</div>
</div>
</div>
<div class="span6">
<div class="alert alert-success">
<div>I am a second success message!</div>
</div>
<div class="alert alert-error">
<div>I am a error message!</div>
</div>
</div>
</div>
</div>
Sometimes it’s needed to show one or more messages after the reload of the page. In that case the messages should be stored in the session.
It’s quite easy.
<?php
# Just use these methods (in your controller) to store the messages into the session
$this->_helper->flashMessenger->addSessionSuccessMessage( 'I am a success message and will be displayed on the next reload of the page!' );
$this->_helper->flashMessenger->addSessionInformationMessage( 'I am a information message and will be displayed on the next reload of the page!' );
$this->_helper->flashMessenger->addSessionErrorMessage( 'I am a error message and will be displayed on the next reload of the page!' );
?>
If you have any questions or suggestions to improve the Extended Flash Messenger feel free to contact me. If you want to contribute just add a new branch and send me a pull request.
Best regards
Magnus