Php 如何使用StreamdResponse在Symfony2中渲染模板视图
我正在尝试使用StreamdResponse将进度输出到Symfony2中的索引页 下面的代码确实显示了api调用的进度,但在实际视图中呈现流式信息时遇到了问题。现在,它只是在页面顶部输出纯文本,然后在全部完成后呈现视图 我不想返回最终数组并关闭函数,直到所有内容都加载完毕,但我似乎无法在输出进度时获得常规的细枝模板 我尝试过使用render,但除非返回,否则似乎没有任何东西能够真正将视图文件输出到屏幕上Php 如何使用StreamdResponse在Symfony2中渲染模板视图,php,symfony,buffer,Php,Symfony,Buffer,我正在尝试使用StreamdResponse将进度输出到Symfony2中的索引页 下面的代码确实显示了api调用的进度,但在实际视图中呈现流式信息时遇到了问题。现在,它只是在页面顶部输出纯文本,然后在全部完成后呈现视图 我不想返回最终数组并关闭函数,直到所有内容都加载完毕,但我似乎无法在输出进度时获得常规的细枝模板 我尝试过使用render,但除非返回,否则似乎没有任何东西能够真正将视图文件输出到屏幕上 public function indexAction($countryCode)
public function indexAction($countryCode)
{
//anywhere from five to fifteen api calls are going to take place
foreach ($Widgets as $Widget) {
$response = new StreamedResponse();
$curlerUrl = $Widget->getApiUrl()
. '?action=returnWidgets'
. '&data=' . urlencode(serialize(array(
'countryCode' => $countryCode
)));
$requestStartTime = microtime(true);
$curler = $this->get('curler')->curlAUrl($curlerUrl);
$curlResult = json_decode($curler['body'], true);
if(isset($curlResult['data'])){
//do some processing on the data
}
$response->setCallback(function() use ($Widget, $executionTime) {
flush();
sleep(1);
var_dump($Widget->getName());
var_dump($executionTime);
flush();
});
$response->send();
}
//rest of indexAction with a return statement
return array(
//all the vars my template will need
);
}
另外,另一个重要的细节是,我正在尝试将所有内容呈现给twig,这似乎有一些有趣的问题。据我所知,您只有一次机会从服务器(PHP/twig)向浏览器输出内容,然后由JavaScript进行任何进一步的更改(如更新进度条) 我建议使用multi-cURL异步执行所有15个请求。这有效地使总请求时间等于最慢的请求时间,因此您可以更快地为页面提供服务,并可能消除对进度条的需要
// Create the multiple cURL handle
$mh = curl_multi_init();
$handles = array();
$responses = array();
// Create and add the cURL handles to the $mh
foreach($widgets as $widget) {
$ch = $curler->getHandle($widget->getURL()); // Code that returns a cURL handle
$handles[] = $ch;
curl_multi_add_handle($mh, $ch);
}
// Execute the requests
do {
curl_multi_exec($mh, $running);
curl_multi_select($mh);
} while ($running > 0);
// Get the request content
foreach($handles as $handle) {
$responses[] = curl_multi_getcontent($handle);
// Close the handles
curl_close($handle);
}
curl_multi_close();
// Do something with the responses
// ...
理想情况下,这将是您的Curler
服务的一种方法
public function processHandles(array $widgets)
{
// most of the above
return $responses;
}
您可以实现<代码> SETCalbBuff< /C>方法中的所有逻辑,因此请考虑此代码:
public function indexAction($countryCode)
{
$Widgets = [];
$response = new StreamedResponse();
$curlerService = $this->get('curler');
$response->setCallback(function() use ($Widgets, $curlerService, $countryCode) {
foreach ($Widgets as $Widget) {
$curlerUrl = $Widget->getApiUrl()
. '?action=returnWidgets'
. '&data=' . urlencode(serialize(array(
'countryCode' => $countryCode
)));
$requestStartTime = microtime(true);
$curler = $curlerService->curlAUrl($curlerUrl);
$curlResult = json_decode($curler['body'], true);
if(isset($curlResult['data'])){
//do some processing on the data
}
flush();
sleep(1);
var_dump($Widget->getName());
var_dump( (microtime(true) - $requestStartTime) );
flush();
}
});
// Directly return the streamed response object
return $response;
}
进一步的阅读和文章
希望此帮助这15个请求是连续的吗?@Rhono,是的,它们是连续的。现在,页面直到全部完成才呈现,所以我尝试在每次调用完成时显示进度。您好@absentx您看了这个答案吗?你觉得怎么样?