沃梦达 / 编程问答 / php问题 / 正文

使用 Laravel 4 更新视图中的实时数据(如进度条)

Update live data (like progress bar) in view with Laravel 4(使用 Laravel 4 更新视图中的实时数据(如进度条))

本文介绍了使用 Laravel 4 更新视图中的实时数据(如进度条)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送数据以在视图中实时更新,例如进度显示操作状态的栏.在 laravel 4 中最好的方法是什么?

I would like to send data to update live in a view, such as a progress bar showing the status of an action. What is the best way to do that in laravel 4?

<小时>

设置

我正在开发一个基于 Laravel 4 的项目,每个用户都可以兑换一个序列号.


The Setup

I'm working on a Laravel 4 based project where each user can redeem a serial key.

我创建了一个管理后台,可以轻松粘贴到密钥列表中,或上传密钥文件.

I've made an admin backend where I can easily paste in a list of keys, or upload a file of them.

假设 $key_string 是我上传的以换行符分隔的键的字符串,并且想要解析出来然后上传包含的键字符串 - 这是添加的简化代码键:

Let's say $key_string is the string of newline-seperated keys that I've uploaded, and want to parse out to then upload the contained key strings from - here is the simplified code that adds the keys:

$key_string = rtrim($key_string);
$key_string = str_replace("

", "
", $key_string);
$keys = explode( "
", $key_string);

foreach($keys as $index => $key) {
    Key::create(
        array( "serial" => trim($key) )
    );
}

由于我上传的密钥集数以千计,这有时可能需要 30 秒,在此期间管理面板自然不会显示任何内容.

Since the sets of keys I upload number in the thousands, this can sometimes take a good 30 seconds, during which time the admin panel naturally doesn't show anything.

现在,我不介意花点时间.我不需要优化上传以使用一个查询等,但我想得到一些实际的反馈,以便我知道上传已经走了多远.

Now, I don't mind it taking this time. I don't need to optimize the upload to use one query, etc, but I would like to have some actual feedback so I know how far the upload has gone.

当我上传密钥时,我希望能够每隔几秒或按百分比更新我视图中的进度条或计数器(使用当前的 $index)

When I upload keys, I would like to be able to update a progress bar or counter in my view every few seconds or percent ticks (using the current $index)

有没有一种简单的方法可以轻松地处理这个问题,最好集成在 Laravel 4 中?我假设这将涉及 ajax,但有人能指出我正确的方向吗?

Is there an easy way to handle this painlessly, preferably integrated in Laravel 4? I'm assuming this would involve ajax, but can someone point me in the right direction?

推荐答案

使用 PHP 确实有两种选择,无需使用 Web Sockets 或 Push-Pull 设置.这并不是真正的 Laravel 东西,它更像是一个请求 JSON东西"的 AJAX 循环.

With PHP there are really two options without going to Web Sockets or Push-Pull setups. This isn't really a Laravel thing it's more of an AJAX loop that requests JSON "thing".

短轮询

Olark 将这种方法用于他们的聊天脚本.

Olark uses this methodology for their chat script.

setInterval(function() {
    $.getJSON("/path", function(data) {
        // update the view with your fresh data
    });
}, 5000);

长轮询

Javascript

var eventName = function() {
    $.getJSON("/path", function(data) {
        // update the view with your fresh data
        if (data.progress < 100)
            eventName();
    });
};

控制器逻辑

当我让用户上传 CSV 并等待它完成上传并被处理时,我会使用它.

I use this when I have users upload a CSV and are waiting for it to finish uploading and be processed.

// in your controller
$check = true;
while ($check) {
    // search database
    // compare values
    if ($newDataWasFound)
        $check = false;

    $progressFromAbove = 90;
}

return Response::json(array(
    'newData' => $array,
    'progress' => $progressFromAbove,
));

我使用 Laravel 3 对此进行了截屏,但长轮询与 PHP 无关,与 Laravel 无关.https://www.youtube.com/watch?v=LDgJF77jELo

I made a screencast on this using Laravel 3 but Long Polling is PHP relevant not Laravel. https://www.youtube.com/watch?v=LDgJF77jELo

示例

  • https://gist.github.com/clouddueling/5239153
  • https://gist.github.com/clouddueling/6296036

这篇关于使用 Laravel 4 更新视图中的实时数据(如进度条)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 Laravel 4 更新视图中的实时数据(如进度条)

基础教程推荐