Is it possible or How can i give a type to a FlashMessage in Zend?
For example
/* This is a \"Success\" message */
$this -> _helper -> FlashMesseng
Method signatures in Zend Framework 1.12.x for FlashMessenger:
public function addMessage($message, $namespace = null)
public function getMessages($namespace = null)
public function hasMessages($namespace = null)
public function clearMessages($namespace = null)
So to set messages the following will work:
/* success message */
$this->_helper->flashMessenger()->addMessage('Post created!', 'success');
/* error message */
$this->_helper->flashMessenger()->addMessage('You have no permissions', 'error');
And for the view, the following should work:
<?php $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
<?php if ($flashMessenger->hasMessages('success')): ?>
<div class="message success">
<?php foreach ($flashMessenger->getMessages('success') as $msg): ?>
<?php echo $msg ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($flashMessenger->hasMessages('error')): ?>
<div class="message error">
<?php foreach ($flashMessenger->getMessages('error') as $msg): ?>
<?php echo $msg ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
It is possible. Sample implementation in described in this blog post:
Excerpt:
class AuthController extends Zend_Controller_Action {
function loginAction() {
. . .
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($this->view->form->isValid($formData)) {
. . .
} else {
$this->view->priorityMessenger('Login failed.', 'error');
}
. . .
}
}
I think the best way to do this is by using the flashmessenger namespaces:
/* success message */
$this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Post created!');
/* error message */
$this->_helper->FlashMessenger()->setNamespace('error')->addMessage('You have no permissions');
And then in your layout you can get the messages added to each namespace:
<?php $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
<?php if ($flashMessenger->setNamespace('success')->hasMessages()): ?>
<div class="message success">
<?php foreach ($flashMessenger->getMessages() as $msg): ?>
<?php echo $msg ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($flashMessenger->setNamespace('error')->hasMessages()): ?>
<div class="message error">
<?php foreach ($flashMessenger->getMessages() as $msg): ?>
<?php echo $msg ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
At one time you used assoc arrays to do this... Im not ure if this is still current or not...
/* This is a "Success" message */
$this -> _helper -> FlashMessenger(array('success' => 'You are successfully created a post.'));
/* This is an "Error" message */
$this -> _helper -> FlashMessenger(array('error' => 'There is an error while creating post.'));
/* This is just a "Notification" message */
$this -> _helper -> FlashMessenger(array('notice' => 'Now you can see your Post'));