Having 2 separate ajax requests (one to monitor the progress of the other) as supplied by answer 1 by Exlord at;
This works on a normal (non-yii) php page,
It also works with yii 1.1.14
But it does not work with Yii 2.0.0 Beta
It should check (and return) a progress status value each 1000ms, but in Yii2 it completes the whole script before returning only the last value ‘9’.
Why?
I used the following code;
Yii 1.1.14 SiteController.php and Yii 2.0.0 beta SiteController.php;
public function actionTask()
{
for ($i = 0; $i < 10; $i++) {
echo($i . "<br/>");
file_put_contents(__DIR__.'/../runtime/progress', $i);
sleep(2);
}
}
public function actionProgress()
{
if (file_exists(__DIR__.'/../runtime/progress') && is_readable(__DIR__.'/../runtime/progress'))
echo file_get_contents(__DIR__.'/../runtime/progress');
else
echo(-1);
}
Yii 1.1.14 views/site/index.php;
<div>Progress: <span id="progress">0</span></div>
<?php
Yii::app()->clientScript->registerScript(‘my-script’,"
//alert('jQuery loaded');
task();
getProgress();
function task() {
$.ajax({
url: 'index.php?r=site/task',
success: function (data) {
console.log('Task is over :' + data);
}
});
}
function getProgress() {
$.ajax({
url: 'index.php?r=site/progress',
success: function (data) {
data = parseInt(data);
if (data != 9) {
console.log('progress :' + data);
$('#progress').html(data);
setTimeout(function () {
getProgress();
}, 1000);
}
}
});
}
",
CClientScript::POS_END);
?>
Yii 2.0.0 beta views/site/index.php;
<div>Progress: <span id="progress">0</span></div>
<?php
use yii\web\View;
$this->registerJs("
//alert('jQuery loaded');
task();
getProgress();
function task() {
$.ajax({
url: 'index.php?r=site/task',
success: function (data) {
console.log('Task is over :' + data);
}
});
}
function getProgress() {
$.ajax({
url: 'index.php?r=site/progress',
success: function (data) {
data = parseInt(data);
if (data != 9) {
console.log('progress :' + data);
$('#progress').html(data);
setTimeout(function () {
getProgress();
}, 1000);
}
}
});
}
",
View::POS_END, ‘my-script’);
?>