Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否有方法将回调作为数据参数传递给Laravel 4.2 Queue::push()_Php_Laravel_Laravel 4 - Fatal编程技术网

Php 是否有方法将回调作为数据参数传递给Laravel 4.2 Queue::push()

Php 是否有方法将回调作为数据参数传递给Laravel 4.2 Queue::push(),php,laravel,laravel-4,Php,Laravel,Laravel 4,我有一些耗时的代码,用于处理我希望在后台运行的一系列HTTP请求的结果。我正在使用Redis存储来管理队列。以下是我尝试过的: Queue::push( 'FetchUrls', [ 'urls' => [ 'http://one.com', 'http://two.com', 'http://three.com' ], 'complete' => function( $response ) { /* process data returned by URL

我有一些耗时的代码,用于处理我希望在后台运行的一系列HTTP请求的结果。我正在使用Redis存储来管理队列。以下是我尝试过的:

Queue::push( 'FetchUrls', [
    'urls'     => [ 'http://one.com', 'http://two.com', 'http://three.com' ],
    'complete' => function( $response ) { /* process data returned by URL here */ },
    'error'    => function( $error    ) { /* process HTTP errors here */ },
]);
Redis队列存储中显示的是
JSON
$data
参数的序列化:

{
    "job": "FetchUrls",
    "data": { 
        "urls": [
            "http:\/\/one.com",
            "http:\/\/two.com",
            "http:\/\/three.com"
        ],
        "complete": [],
        "error": []
    },
    "id": "aAlkNM0ySLXcczlLYho19TlWYs9hStzl",
    "attempts": 1
}

如您所见,回调在队列存储中显示为空数组。我以前从未使用过Queue类,因此可能以错误的方式处理此问题。我正在寻找解决这个问题的最佳方法的建议。谢谢

为了安全起见,您应该只推送数组(因为序列化有问题)。

要回答您的问题-没有解决办法,您应该重新考虑逻辑。

为了安全起见,您应该只推送数组(因为序列化有问题)。

要回答您的问题-没有解决办法,您应该重新考虑逻辑。

您可以传递函数名,然后使用类似于
call\u user\u func()的方法调用它们

这种方法没有基于类的版本,但相对来说比较干净


call\u user\u func()
使用
['ResponseHandler','fetchComplete']
作为第一个参数将调用
ResponseHandler::fetchComplete()

您可以传递函数名,并使用类似于
call\u user\u func()的方法调用它们

这种方法没有基于类的版本,但相对来说比较干净


使用
['ResponseHandler','fetchComplete']
作为第一个参数调用\u user\u func()
,将调用
ResponseHandler::fetchComplete()

谢谢!这就是我一直在寻找的解决方案!非常感谢。这就是我一直在寻找的解决方案!
Queue::push('FetchUrls', [
    'urls'     => ['http://one.com', 'http://two.com', 'http://three.com'],
    'complete' => ['ResponseHandler', 'fetchComplete'],
    'error'    => ['ResponseHandler', 'fetchError'],
]);

class FetchUrls
{
    public function fire($job, $data)
    {
        list($urls, $complete, $error) = $data;

        foreach ($urls as $url) {
            if ($response = $this->fetch($url)) {
                $job->delete();
                call_user_func($complete, $response);
            } else {
                $job->release();
                call_user_func($error, $this->getError());
            }
        }
    }

    private function fetch($url)
    {
        // ...
    }

    private function getError()
    {
        // ...
    }
}

class ResponseHandler
{
    public static function fetchComplete($response)
    {
        // ...
    }

    public static function fetchError($error)
    {
        // ...
    }
}