Answer the question
In order to leave comments, you need to log in
How to make progress bar executing php function in Yii2?
The task is this:
Through the form I send the page url to the server, it is parsed, in the controller inside the function, manipulations with the dom tree of the page take place - I pull out everything I need from the page. For the sake of an example, pull out all the links from the page and put them in an array. The situation is as follows:
In the view, to send data, I use ActiveForm and Pjax, the view turns to the index action, the Paresepage function is called from there, where I get all the links from the page, then in the same function I process the links in a cycle - I form the correct url's , remove broken links, check the response from the server, etc.
The process performed in the function turns out to be quite time-consuming. You need to do the following:
When processing each link inside the loop, display to the user how many links have already been processed, i.e. make a progress indicator. how to implement this?
View:
<?php Pjax::begin(['id' => 'pjax']); ?>
<?php $form = ActiveForm::begin(['id' => 'analise-form', 'method' => 'post',]); ?>
<?= $form->field($model, 'site')->textInput(['maxlength' => true]); ?>
<div class="form-group">
<?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<pre>
<?php if(isset($url) && isset($page_item)){
print_r($url);
print_r($page_item);
} ?>
</pre>
<?php Pjax::end(); ?>
public function actionIndex()
{
$model = new AnaliseForm();
$url = '';
$page = '';
$page_item = '';
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$url = Yii::$app->request->post();
$url = htmlspecialchars(strip_tags($url['AnaliseForm']['site']));
$page_item = AnaliseController::actionParesepage($url); //функция анализа ссылок на странице
}
return $this->render('index', [
'model' => $model,
'url' => $url,
'page_item' => $page_item,
]);
}
Answer the question
In order to leave comments, you need to log in
I don’t know how in yii, but in php I would solve this by creating a queue (the controller creates a task and immediately returns a response to the user, then it is executed by the worker in the background) and then either checking the current status via ajax once a minute from the client side, or in queue handler sending notifications about the current status to the client via websockets.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question