Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 return 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.

like image 736
Dan Avatar asked Dec 28 '22 23:12

Dan


2 Answers

You can fetch the FlashMessenger from your model like this:

$messenger = Zend_Controller_Action_HelperBroker::getStaticHelper('flashMessenger');
$messenger->addMessage('test message');
like image 175
Goran Jurić Avatar answered Jan 14 '23 14:01

Goran Jurić


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).

like image 33
Gordon Avatar answered Jan 14 '23 13:01

Gordon