0

I need to be able to access a number of routes only if a condition is met. Else I should not have access to those routes.

I thought this should be done with a filter but I think I'm missing something about how they work.

So this is my filter:

Route::filter('my.filter', function()
{
    //some code regarding said condition
    if($mycondition==true){
        //WHAT TO PUT HERE?
    }else{
        //Error message
    }
}

And in my routes I will have:

Route::group(array('before' => 'my.filter'), function()
{
    Route::resource('cities', 'CitiesController');
    //... many more controllers here
});

But all the examples I have seen have a redirect inside the filter in the if part. I don't want that, I only want, if the condition in the filter is true, you get to see that url.

7
  • Well the question is, what do you want the user to see if the condition is false? One possibility would be to throw a HTTP 403 Error (Access Denied)... Commented Oct 20, 2014 at 14:01
  • yes, that is fine with me, but, how to do that? Commented Oct 20, 2014 at 14:02
  • Just a second, i'm gonna write an answer Commented Oct 20, 2014 at 14:03
  • You should return true if the condition is met within your filter Commented Oct 20, 2014 at 14:04
  • Jonathon, whatever I have return in the filter will output on the browser, I don't want that. I want the page for the route. Commented Oct 20, 2014 at 14:05

2 Answers 2

2

If you wanna have your filter stop further Execution of your routes (and controllers and so on) you have to:

  • Return something (e.g. a Redirect)
  • or throw an Exception

In Laravel you can do this pretty easily:

Route::filter('my.filter', function()
{
    //some code regarding said condition
    if($mycondition==true){
        // here you have to do nothing so you could also flip the if...
    }else{
        App::abort(403);
    }
}

Official Docs

Little sidenote: As mentioned above in the comment i would flip the if. Like that:

Route::filter('my.filter', function()
{
    //some code regarding said condition
    if($mycondition==false){
        App::abort(403);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are right here, but abort doesn't work, return Response::make('Unauthorized', 401); did the trick in my case. The return true was messing things up.
0

You don't have to put anything there, just let the function return void. That will tell laravel that the user is allowed to see the page. You can always put a return there yourself to let the user see the page and skip all the following conditions.

When the condition is not met, it is better to redirect the user to a page where you explain why the user isn't able to see the page.

Route::filter('my.filter', function()
{
    //some code regarding said condition
    if($mycondition==true){
        return;
    }else{
        //Error message
    }
}

4 Comments

Did you replace $condition with a real condition?
Mmmh, it seems my problem is somewhere else inside the controllers I'm testing, looks like your solution works for the filtering part, just need to solve the other issue.
No this solution doesn't work like you would expect. A return from a filter is like if you would return something from your route. So to "let the request through" you need to return nothing. if you wanna block it return something or throw an Exception. (see my answer)
Mmmh, I get The Response content must be a string or object implementing __toString(), "boolean" given. when I do a test with a simpler controller.

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.