Check against multiple params in Route::filter()

Submitted by bgallagh3r - 5 years ago

Route::filter() passes multiple filter params as individual values like function($route, $response, $param1, $param2 ...) this is an easy way to check against multiple params. If I'm checking permissions like this ->before('permission:user,moderator') this makes it easy to check against. Since Route::filter() passes $route and $response in a BEFORE filter the array_slice strips them out of the array to allow you to check against the values you want.

Route::filter('permission', function(){
    $haystack = array_slice(func_get_args(),2); // Set to 1 if using on AFTER filter
    $needle = Auth::user()->role->role;
    if (!in_array($needle, $haystack))
        App::abort(403, 'Not Allowed');
});