doujiu8918 2012-12-01 08:56
浏览 103
已采纳

在Laravel中使用路由过滤器

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?

  • 写回答

1条回答 默认 最新

  • dongzhashou0116 2012-12-01 09:12
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog