【发布时间】:2014-11-14 16:25:22
【问题描述】:
我有一个关于为多个用户角色过滤控制器及其操作的问题。假设我有一个名为 MyController 的控制器:
public class MyController extends \BaseController
{
public static function index()
{
}
public static function show()
{
}
public static function create()
{
}
public static function store()
{
}
public static function other()
{
}
}
我为每个角色设置了 2 个过滤器,分别命名为 admin 和 staff:
Route::filter('admin', function()
{
// Lines of code to get role
if($role != 'admin') return View::make('errors.401');
});
Route::filter('staff', function()
{
// Lines of code to get role
if($role != 'staff') return View::make('errors.401');
});
然后,我尝试在 MyController 的构造函数上使用 beforeFilter :
public function __construct()
{
$this->beforeFilter('admin', ['only' => ['index', 'show', 'create', 'store']]);
$this->beforeFilter('staff', ['only' => ['index', 'show']]);
}
当我添加第一个 beforeFilter 时,它按预期工作(当我以员工身份登录到我的应用程序时,我无法访问 index、show、create 和 store 方法)。但是当我添加第二个过滤器并再次以员工身份登录时,我无法访问索引和显示操作,我希望员工可以访问。
我的问题是,是否可以在控制器的构造函数中为多个角色定义过滤器? (在这种情况下,我想让管理员和员工可以访问操作索引和显示,但创建和存储只能由管理员访问)如果可能,我该如何实现?
谢谢。
【问题讨论】:
标签: laravel laravel-4 filter acl before-filter