Zend Framework: Flash Messenger, add a message from the model
Any idea on how best to add a message to flash messenger from the model?
As FlashMessenger is an action helper, this seems not to be possible, so the obvious solution is to create an internal message object in the model, and return that to the controller from where you can use addMessage(). But if you want to retu开发者_如何学JAVArn something else as well, this falls down.
Another idea is an additional session namespace for these internal messages, which is then merged in with the Flash Messenger namespace messages at output time?
Anyone have any thoughts or experience on this? Cheers.
You can fetch the FlashMessenger from your model like this:
$messenger = Zend_Controller_Action_HelperBroker::getStaticHelper('flashMessenger');
$messenger->addMessage('test message');
Both routes sound valid.
One idea would be to add a $message arrayto your model, which the action helper can access to retrieve the messages. This way you could return multiple messages at once.
Another option would be to use a Subject/Observer pattern. Have your models implement the Subject interface and when you need to output messages, notify the observers, e.g. the flash messenger (for which you would have to implement the Observer interface).
精彩评论