6

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:

  • Yes, i'm sure this is the problem. I replaced the long-running procedure with 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.
  • I can't make the script execute faster. Instead of the sleep(11) in the example, in production code something is sent to a printer which takes ~11 seconds to complete.
Kaii
  • 20,122
  • 3
  • 38
  • 60
  • any ideas appreciated! – Kaii Mar 28 '14 at 14:11
  • What's in `flashMessenger`? What does `addMessage` do? Write to db or session? `var_dump()` and `exit` are your friends =) I can't find `addMessage()` in [`FlashMessenger`](https://github.com/zendframework/zf2/blob/bbb0545ad6552b9ecadd96c12223022b38734686/library/Zend/View/Helper/FlashMessenger.php) – Rudie Mar 30 '14 at 18:05
  • I'm confused about your ZF2 API reference and ZF2 Tag. Your code looks like ZF1! – Mamuz Mar 30 '14 at 20:42
  • FlashMessenger is Session based, please check your session configurations. I bet both examples works fine. Please dump the message container after adding, you will see both examples works fine – Mamuz Mar 30 '14 at 20:50
  • @Mamuz i will test this tomorrow. i also assume that after addding the message appears in the container in both cases. the problem is that it's not displayed on the next page, as if the message had expired for the longer running request – Kaii Mar 30 '14 at 20:53
  • "Mamuz what makes it look like ZF2?" => Zend_Controller_Action is replaced by Namespacing and Underscore as prefix for protected methods is not used in ZF2 – Mamuz Mar 30 '14 at 20:53
  • Looks like http://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.flashmessenger – Mamuz Mar 30 '14 at 20:59
  • @Mamuz you're right. i just looked into the code and realized it is in fact ZF1. I was told our application was written in ZF2 - sorry, i didn't verify this. – Kaii Mar 30 '14 at 21:06
  • If the time is too long, only messages that are not displayed or is the whole page is blank? And what is your exact version of Zend? – doydoy44 Mar 31 '14 at 16:10

2 Answers2

3

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.

Community
  • 1
  • 1
Mamuz
  • 1,730
  • 12
  • 14
  • good suggestion, i will try if forwarding instead of redirecting makes a difference for the flashMessenger. Regarding your queuing suggestion: it's funny, because i had to replace the "adding to queue" with actual printing logic (which obviously takes much longer). `sleep(1)` is the mockup for placing the job in the queue (which returns almost instantly) and `sleep(11)` is the mockup for the real printing logic. it returns when the printjob is done, and this takes a while. The flashMessenger() should display if printing was successful or not. (i.e. socket timeout while connecting to printer) – Kaii Mar 31 '14 at 07:35
  • When forwarding instead of redirecting the message appears. we now have forwarding in the application as a workaround. I'm still looking for the cause of the redirect issue, which i would like to solve. (forwarding is not the same as redirecting, i want the user to see the correct URL, which forwarding can't do). I will award the bounty to an answer that shows the real issue. – Kaii Apr 01 '14 at 10:12
-1

I think you should play in js not in php.

sleep() Just stop the execution for given number of seconds.

When you are using sleep(1) or sleep(11) both will give the same output.

I think you are dispatching some file with code given below.

$this->_redirect('/attachment/index');

You should send a special message(To identify to load after 11 sec) that is not being used in other message and you should write js to be executed(hide message for now and show after 11 seconds) after 11-seconds whenever that message is being received in.phtml file for message.

Amit Garg
  • 3,867
  • 1
  • 27
  • 37