我需要立即响应一些变量并继续在 Zend Framework 2 中执行另一个进程,这段代码可能会有所帮助:
public function wstestsAction(){
//This variables needs to be responded immediately
$response["response"] = true;
$response["message"] = "Msg Test";
//This process could take 1 minute
$libraryInstance = new libraryInstance();
$sendData = $libraryInstance->sendData("params");
$varsToView["resultJson"] = 'Zend'Json'Json::encode($response);
$viewModel = new ViewModel($varsToView);
$viewModel->setTerminal(true);
return $viewModel;}
在视图(wstests.phtml)中,我有以下代码:
<?=$this->resultJson?>
谢谢!!!
它工作得很好。
public function wstestsAction(){
//This variables needs to be responded immediately
$response["response"] = true;
$response["message"] = "Msg Test";
//----------------------------------------//
ob_end_clean();
header("Connection: close");
ignore_user_abort(); //optional
ob_start();
//----------------------------------------//
$varsToView["resultJson"] = 'Zend'Json'Json::encode($response);
$this->layout('layout/json');
$viewModel = new ViewModel($varsToView);
$viewModel->setTemplate('application/test/wstests.phtml');
$viewRender = $this->getServiceLocator()->get('ViewRenderer');
$html = $viewRender->render($viewModel);
echo $html;
//----------------------------------------//
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
session_write_close(); // Added a line suggested in the comment
//----------------------------------------//
//This process could take 1 minute
$libraryInstance = libraryInstance();
$sendData = $libraryInstance->sendData("params");
}
我建议使用 cron 任务来完成需要一段时间的进程。 您需要将要发送的参数存储在某种类型的存储(甚至是数据库)中。
从那里,您可以每 5 分钟运行一次 cron 任务来检查要提交/处理的新数据(使用 PHP)。
如果您希望页面的呈现速度与其他页面一样快,这将是理想的途径。
希望这有帮助。