Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sunrise's avatar

How to control role's access to different path?

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?

0 likes
1 reply
martinbean's avatar

@sunrise Create a middleware class that checks the role and apply it to a route group:

Route::group(['middleware' => 'web'], function () {
    Route::auth();

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

Please or to participate in this conversation.