在长请求后添加Message时flashMessenger无法正常工作

Kai*_*aii 6 php zend-framework php-5.4

我正面临Zend Frameworks flashMessenger一个非常奇怪的问题,无法找出问题的原因,也不知道如何解决问题.

当请求的操作需要很长时间时,flashMessenger无法按预期工作.

非工作示例:

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,后面的控制器操作/attachment/index不会显示flashMessage.

但是,如果我减少脚本的运行时间,它的工作原理:

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:flashMessenger不显示我的消息的原因是什么?我怎么能解决这个问题?

笔记:

  • 是的,我确定这是问题所在.我插入了sleep(11)生产代码,它产生了描述的行为.问题不是由于我为了隔离案例而替换的代码引起的.它真的是由脚本的长运行时间引起的.
  • 我不能让脚本执行得更快.sleep(11)在生产代码中,不是在示例中,而是将某些内容发送到打印机,这需要大约11秒才能完成.

Ami*_*arg -1

我认为你应该用js而不是php来玩。

sleep() 只需停止执行指定的秒数即可。

当您使用sleep(1)or时,sleep(11)两者都会给出相同的输出。

我认为您正在使用下面给出的代码发送一些文件。

$this->_redirect('/attachment/index');
Run Code Online (Sandbox Code Playgroud)

您应该发送一条特殊消息(用于识别在 11 秒后加载),该消息未在其他消息中使用,并且您应该编写要在 11 秒后执行的 js(暂时隐藏消息并在 11 秒后显示)。正在.phtml 文件中接收消息。