【问题标题】:Laravel 5.2 and `Zizaco/entrust` : How to control role's access to different path?Laravel 5.2 和 `Zizaco/entrust` : 如何控制角色对不同路径的访问?
【发布时间】:2016-04-28 04:36:04
【问题描述】:

我正在使用 Laravel 5.2 和 Zizaco/entrust 5.2.x。
如何控制角色对不同路径的访问?
例如:
admin的路由是这样的:

Route::group(['middleware' => 'web'], function () {
    Route::auth();
    Route::group(['prefix' => 'admin','namespace' => 'Admin'], function () {
        Route::resource('dashboard', 'DashboardController');
     });
});

我希望角色 admin 可以访问 http://www.example.com/admin/dashboard/
其他角色无法访问它, 我该怎么办?

【问题讨论】:

标签: php laravel


【解决方案1】:

您可以在中间件中检查管理员角色。由于您使用的是 zizaco/entrust,因此您可以使用 hasRole 检查角色。

在中间件 web 中使用此代码。

 public function handle($request, Closure $next)
    {
        if (!$user->hasRole('admin')) {
            return redirect('home');//Redirect to any page you wish.
        }
    }

【讨论】:

    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 2016-07-28
    • 2019-10-14
    • 2014-05-05
    • 2017-02-05
    相关资源
    最近更新 更多