Laravel 4:传递数组进行过滤

From my understanding, we can pass argument to filter via this:

Route::filter('age', function($route, $request, $value)
{
    //
});

Route::get('user', array('before' => 'owner|age:200', function()
{
    return 'Hello World';
}));

But, how can I pass array to the filter? For example, I want to pass "cars, speed boat, condominium" to the owner filter. The number of items in the array is dynamic, depending on the route. How is it possible to do that?

Thank you.

You can not pass array (might possible in Laravel 4.1) in Laravel 4.0. However, you can pass more than one parameters in a filter and ensure they are separated by a comma (,).

For example:

Route::filter('age', function($route, $request, $age, $gender, $name)
{
    if ($age < 200 && $gender == 'male' && $name = 'anam' ) {
        return "Welcome to Laravel. Enjoy the awesome!";
    }
});

Route::get('user', array('before' => 'owner|age:200,male,anam', function()
{
    return 'Hello World';
}));

I do this tricks:

Route::filter('filtername', function($route, $request, $value)
{
    $array = explode('-',$value); // use - for delimiter
    // do whatever here
});

And on the routes, use like this

Route::get('user', array('before' => 'filtername:item1-item2-item3', function()
{
    return 'Hello World';
}));

Hope this helped.

Like Anam said, you can give multiple parameters separated by a comma. You can give multiple parameters and turn them back into an array with func_get_args().

array_except is a laravel function to delete certain keys from an array.

Route::filter('age', function($route, $request, $value)
{
    $params = array_except(func_get_args(), array(0, 1));
    if(count($params) > 3) {
        // do something
    }
});