2

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.

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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';
}));

Comments

1

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
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.