7

how i can call filter in filter using laravel?

i have this filter:

Route::filter('auth', function()
{
    if (Auth::guest())
    {
        if (Request::ajax())
        {
            return Response::make('Unauthorized', 401);
        }
        else
        {
            return Redirect::guest('login');
        }
    }
});

now i create another filter called admin and i want call auth filter in this:

Route::filter('admin', function(){
    #call auth filter

    #code    
});

it's possible do that?

4
  • Why would you do that? Did you consider attaching that filter the route the same way as auth filter? Commented Jul 31, 2014 at 14:54
  • i know this solution, but are not the best for my implementation... because i have a lot of filter needed call by other filter, and some time i need to disable that dinamically, passing an arguments... p.s. sorry for my bad english Commented Jul 31, 2014 at 14:57
  • 1
    For advanced filtering, you may wish to use a class instead of a Closure. Since filter classes are resolved out of the application IoC Container, you will be able to utilize dependency injection in these filters for greater testability. laravel.com/docs/routing#route-filters at the bottom of section Commented Jul 31, 2014 at 15:01
  • 2
    What are you trying to do? If you have code that's called by a bunch of different filters, try writing a library class. If you want to call a bunch of different filters, it's best to chain them together on a route or use @delmadord's suggestion of a filter class. It looks like you're headed for a lot of pain if you set up a bunch of interrelated filters where A calls B, B calls C, etc. The dependencies are going to be awful to maintain. Commented Jul 31, 2014 at 15:58

2 Answers 2

5

Using callRouteFilter() method in Illuminate\Routing\RouteFiltererInterface as @Dwight suggested:

Route::filter('admin', function ($route, $request) {
    if ($r = Route::callRouteFilter('auth', array(), $route, $request)) {
        return $r;
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try using the callRouteFilter() method.

Route::filter('admin', function()
{
    Route::callRouteFilter('auth');
});

1 Comment

callRouteFilter requires the first 4 parameters. This would never work.

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.