flashMessenger not working when adding Message after a long request
Asked Answered
S

2

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.
Shala answered 28/3, 2014 at 13:37 Comment(9)
any ideas appreciated!Shala
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 FlashMessengerMetamorphic
I'm confused about your ZF2 API reference and ZF2 Tag. Your code looks like ZF1!Inhaler
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 fineInhaler
@Inhaler 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 requestShala
"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 ZF2Inhaler
Looks like framework.zend.com/manual/1.12/en/…Inhaler
@Inhaler 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.Shala
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?Cheddar
I
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.

Inhaler answered 30/3, 2014 at 21:44 Comment(2)
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)Shala
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.Shala
T
-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.

Thermoluminescent answered 30/3, 2014 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.