0

I am using Laravel 5.2 and Zizaco/entrust 5.2.x .
How to control role's access to different path?
For example:
The route of admin is like this:

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

I would want that the role admin can access http://www.example.com/admin/dashboard/ ,
other roles cann't access it, What should I do?

zwl1619
  • 4,002
  • 14
  • 54
  • 110
  • Try laravel's [middleware](https://laravel.com/docs/master/middleware) – Basheer Kharoti Apr 25 '16 at 17:49
  • Check this it may be helpful [Zizaco roles permissions and middleware](http://stackoverflow.com/questions/36152456/laravel-5-2-integrate-entrust-package-create-role-and-permissions-and-access-i/36155221#36155221) – Veerendra Borra Apr 28 '16 at 11:14
  • Check below url it may help you [Zizaco roles permissions](http://stackoverflow.com/questions/36152456/laravel-5-2-integrate-entrust-package-create-role-and-permissions-and-access-i/36155221#36155221) – Veerendra Borra Apr 28 '16 at 11:15

1 Answers1

1

You can check for the admin role in middleware. Since you are using zizaco/entrust you can check the role using hasRole.

Use this code in middleware web.

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