Yii框架异步请求 [英] Yii framework async request

查看:668
本文介绍了Yii框架异步请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有AJAX请求做3任务:

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.

这是不好的用户体验。

This is bad user experience.

我希望用户提交自己的数据,我的控制器和之后,他将获得成功或失败的消息。并且该过程将完全在服务器侧和它应支持多会话

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.

我发现这样做的最好的解决办法: 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.

这篇关于Yii框架异步请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆