I'm facing a really strange issue with Zend Frameworks flashMessenger and can't find out what's the cause of the problem, nor how to solve it.
When a requested action takes very long, the flashMessenger doesn't work as expected.
Non-working Example:
class AttachmentController extends Zend_Controller_Action {
public function printAction() {
// action takes really long and causes flash message to not appear at all
sleep(11);
$this->_helper->flashMessenger
->setNamespace('success')
->addMessage("It's done!");
$this->_redirect('/attachment/index');
}
}
With the above code, the controller action behind /attachment/index
does not display the flashMessage.
However, if i reduce the runtime of the script, it works:
class AttachmentController extends Zend_Controller_Action {
public function printAction() {
// now the action runs faster and the flashMessage appears!
sleep(1);
$this->_helper->flashMessenger
->setNamespace('success')
->addMessage("It's done!");
$this->_redirect('/attachment/index');
}
}
Question: What may be the cause for the flashMessenger to not display my message? How may i fix that?
Notes:
sleep(11)
in production code and it produces the described behaviour. The problem is not caused by the code i replaced for isolating the case. It's really caused by the long runtime of the script.sleep(11)
in the example, in production code something is sent to a printer which takes ~11 seconds to complete.Both examples works fine. If sleep(11)
doesn't work you are having side-effects, maybe by concurrency. FlashMessenger is session based so you have to check your session configurations. The redirector action helper $this->_redirect();
will send a header and create a new HTTP Request to produce a new entire dispatch process. So you have also to check your plugins which are registered to this process (explained here).
To resolve this problem you can try to use the forwarder instead of redirector. Really good explained here (difference-between-redirect-and-forward) and here (use-flashmessenger-without-redirect).
By the way it is bad practice using sleep
to mock "printjob is done". You should prefer another message like "Printjob added to queue", which is not a lie and it's user friendly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With