Y
Y
yazux2016-05-03 08:09:59
Yii
yazux, 2016-05-03 08:09:59

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(); ?>

And the code that takes the form and turns into a parse function:
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,
        ]);
    }

Now, after submitting the form, the page takes a long time to load and gives out the entire answer at once - a completely ready-made array. But it is necessary to make it so that progress is displayed, and even better, display 1 link to the pages in the order of processing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Evgrafovich, 2016-05-03
@Tantacula

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 question

Ask a Question

731 491 924 answers to any question