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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题