Today i was looking for solution similiar to symfony: setFlash() message. I found in Zend documentation that there is something like FlashMessenger helper. There was even some useful example, so i decided to put it all together and show you how to use that helper with our Zend Framework Structure.
For those who don’t know what are flash messages for, i will give simple example.
Imagine situation that you are adding new item. You are processing form, saving data and redirecting to main/list page. But what about user-friendly message telling that item was successfully saved? We can set some parameters and then check their values but there is better way - flash message. We can easily do something like setFlashMessage and redirect user to other page. Then all we need to do is to check in our template if any flash message is set and put in on display.
To do that we need to add the follwoing code to our Core_Controller_Action:
<?php
public function init() {
# setting up flash messenger
$this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
...
// rest of code
}
## New methods
// this one we will use in our ontrollers
protected function setFlashMessage( $message )
{
$this->_flashMessenger->addMessage( $message );
}
protected function assignFlash(){
$messages = $this->_flashMessenger->getMessages();
$this->view->flashMsg = $messages[0];
}
public function postDispatch()
{
parent::postDispatch();
$this->assignFlash();
}
?>
This is quite good but we can set just one message. There is a way to set many messages but without any key (to associate messages with theirs keys we would have to modify more code) - in Symfony we can set many messages (and each message has its own key):
<?php
$this->getUser()->setFlash('msg1', 'some message');
$this->getUser()->setFlash('msg2', 'some other message');
?>
In my opinion it’s better idea.
In our example to set flash message in Controller we just call setFlashMessage:
<?php
$this->setFlashMessage('Our custom Message');
# then we can redirect to main/list page
?>
Then in template:
{if isset( $flashMsg ) }
<div class="success_list">
{$flashMsg}
</div>
{/if}
That’all. Easy and usefull. If there is better way to play with FlashMessenger that i don’t know - please leave a comment.
Great! Thank you very much!
I always wanted to write in my blog something like that. Can I take part of your post to my blog?
Of course, I will add backlink?
Regards, Timur Alhimenkov
Hi,
It would be also really nice if you add backlink.
thanks. Of course you can take a part
Cheers Kamil
wywołanie
$this->_helper->getHelper(’FlashMessenger’);
powoduje wyświetlenie (dokładnie to zapisanie do pliku error_log5) u mnie komunikatu:
ini_set() has been disabled for security reasons in /home/poligons/library/Zend/Session.php on line 197
da się to jakoś obejść??
Dostajesz jakieś błędy na ekranie czy tylko loguje je do pliku? Daj więcej szczegółów na maila. Prawdopodobnie to problem konfiguracji.
server loguje do pliku, bo takie ma domyslne ustawienia, niestety to nie dedyk i nie da siÄ™ tego zmienic, bo nie mam dostÄ™pu do pliki php.ini… czy da siÄ™ jakoÅ› skonfigurować ZF tak, aby Zend Session nie wywoÅ‚ywaÅ‚o takiego błędu?