Using route filters in Laravel
Asked Answered
C

1

6

I'm trying to use route filters in laravel to check whether a specific user has an access to a page:

Route::filter('check_roles', function()
{
    $current_url = URI::current();
    $access = 0;
    $nav = Session::get('navigation');
    foreach($nav as $k => $n){
      if(in_array($current_url, $n)){
        $access = 1;
      }
    }

    if($access == 0){
     return Redirect::to('home');
    }
    //problem is if the user has access to the page a blank page is returned

});

I'm using it in a route like this:

Route::get('admin/(:all)', array('before' => 'check_roles'));

The problem is if the user has access to the page a blank page is returned. How do I continue on with the default controller action if the user has access?

Chaco answered 1/12, 2012 at 8:56 Comment(0)
F
11

Replace Route::get() with Route::filter('pattern: admin/*', 'check_roles');.

Now every time a request contains this pattern will be calling your check_roles filter. I think that this is what you currently need and not a Route::get(),

You could use Route::get() on individual pages like

Route::get('supersecret', array('before' => 'check_roles'), function() { return View::make('mysecret') });

For more info Routing - Filters

Updating to reflect my suggestion on the comment.

You can create an Admin_Controller that will extends your Base_Controller and have your auth filter in the __construct().

class Admin_Controller extends Base_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->filter('before', 'auth');
    }
}

Have this contoller registered in your start.php (search for Autoloader, where your base_controller is mapped).

And you could now extends your Admin_Controller whenever you want to protected your area.

class Pages_Controller extends Admin_Controller {
    // do cool stuff
 }

Hope that helps

Flouncing answered 1/12, 2012 at 9:12 Comment(5)
I'm doing route::filter with the auth filter. When I tried doing something like this: Route::filter('pattern: admin/*', 'auth|check_roles'); it only executes the first one.Chaco
I think maybe the problem here is that laravel isn't executing the method that I have in the controller so its doing nothing. I've already use Route::controller(Controller::detect()); to create a route for every controller but it doesn't seem to be taking effect.Chaco
Multiple filters can be attached in REST routes, i dont think you can create a filter that uses multiple filters. What you might need is to create a new Controller and attach to that your Controller your auth filter. Then extends that Controller whenever you need your users to provide a password for accessing that part of your appFlouncing
Route::controller(Controller::detect()) it been reported that it screws up the routes, i suggest you manually add the controllers you need to be routed like, Route::controller(array('admin', 'members', pages', 'and_so_on'));Flouncing
thanks! I've just included the check_roles filter using Route::filter('admin/*', 'check_roles') and the auth filter on the controller.Chaco

© 2022 - 2024 — McMap. All rights reserved.