如何在 Laravel 中运行异步脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26581120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:20:06  来源:igfitidea点击:

How to run asynchronous script in Laravel?

phplaravel

提问by aceraven777

I have a controller, lets say 'Foo' controller with function 'index' that takes a long time to execute.

我有一个控制器,可以说'Foo' 控制器带有函数'index' 需要很长时间才能执行。

I have a another controller, 'Bar' controller in which i want to process / call the 'index' function in the 'Foo' controller asynchronously.

我有另一个控制器“Bar”控制器,我想在其中异步处理/调用“Foo”控制器中的“index”函数。

class BarController extends \BaseController {
    public function index()
    {
        // call the Foo controller here
    }
}

Is there a way to call the 'index' function in the 'Foo' controller asynchronously?

有没有办法异步调用“Foo”控制器中的“index”函数?

回答by Laurence

This is exactly what Laravel Queuesare for. Move your command into a library somewhere, and call a method to 'queue' the command. Then it will be executed while your original controller can return to the user.

这正是Laravel 队列的用途。将您的命令移动到某个库中,并调用一个方法来“排队”命令。然后它将被执行,而您的原始控制器可以返回给用户。