用于过滤的Laravel Pass数组参数

时间:2014-11-16 22:37:24

标签: php arrays laravel laravel-4 laravel-filters

我需要将数组传递给控制器​​构造函数中的过滤器。问题是要将参数传递给过滤器:

$this->beforeFilter('filterName:param1,param2');

要传递一个数组,我需要json_encode使数组成为一个用逗号分隔的字符串。所以以下命令:

$array = ['this', 'that', 'other'];
$this->beforeFilter('filterName:' . json_encode($array));
// The above line basically becomes:
// $this->beforeFilter('filterName:["this","that","other"]');

将传递给我的过滤器:

Route::filter('filterName', function($route, $request, $param1, $param2, $param3)
{
    // $param1 is ["this"
    // $param2 is "that"
    // $param3 is "other"]
});

由于我不知道我的阵列有多大,我无法重建阵列。我该怎么办?

更新

我能做到的一件事是

Route::filter('filterName', function($route, $request, $value)
{
    $params = array_except(func_get_args(), array(0, 1));
    if(count($params) > 3) {
        $params = json_decode(implode(",", $params));

        // Now use $params
    }
});

这适用于数组。但如果我传递一个嵌入式数组,结果就变成stdObject,这没关系,但我不喜欢我传递一个数组并得到一个对象的事实。

0 个答案:

没有答案
相关问题