Yii 框架异步请求

2022-01-04 00:00:00 asynchronous php ajax yii

我有执行 3 个任务的 ajax 请求:

I have ajax request that do 3 missions:

  • 保存模型 (DB)
  • 发送电子邮件
  • 给出成功或失败的消息.

因为这个任务太耗时了.用户最多可以等待 20 秒的响应(成功或失败消息).如果用户关闭浏览器,它会停止用户当前进程的操作之一.

Because this mission takes too time. User can wait up to 20 sec for response (success or failed message). And if the user close the browser its stop in one of the operation that current process for the user.

这是糟糕的用户体验.

我希望用户将他的数据提交给我的控制器,然后他会收到成功或失败的消息".并且该过程将完全在服务器端,并且应该支持多会话.

I want user submit his data to my Controller and after it he will get the "success or failed message". And the process will be completely in the server side and its should support multi sessions.

我该怎么做?

推荐答案

@hakre 你给的没有减少用户等待响应的时间.

@hakre What you gave not reduce the time user wait for respond.

我找到了最好的解决方案:yii 的 runactions 扩展

I found the best solution for this: runactions extension for yii

这是让你从控制器后台操作运行的扩展.有几种方法可以使用它.最适合我的情况是这个

This is extension let you run from controller background actions. There are several way to use it. The best one for my case is this

public function actionTimeConsumingProcess()
{
    if (ERunActions::runBackground())
    {
       //do all the stuff that should work in background
       mail->send()
    }
    else
    {
        //this code will be executed immediately
        //echo 'Time-consuming process has been started'
        //user->setFlash ...render ... redirect,
    }
  //User information
  echo "Submit Success!"
}

它的工作但没有 ajax 请求,当我发出 ajax 请求时,由于某种原因它不起作用.所以我用了:

And its work but without ajax request, When I make ajax request its not working for some reason. So I used:

                         ERunActions::httpPOST($this->createAbsoluteUrl('Form/Submit'), array('to'=>'mail@domain.com', 'subject'=>'This is the subject'));

它的工作很棒,但不是理想的解决方案.

And its work great but its not the ideal solution for this.

相关文章