Mustabshir Khan
Mustabshir Khan

Reputation: 21

Same route in multiple role and route group?

I have route which I want to authorize on more than 1 one role.I have created 2 route group one is admin and other one is employee and there is a route abc.com/abc which I want to accessible on both roles. Admin routes are:

Route::group(['middleware'=>['auth','role:admin|hr-manager|manager ']],function(){
    Route::get('employee',['as'=>'employee','uses'=>'EmployeeController@employeeList']); 
    Route::get('leave-type',['as'=>'leave.type','uses'=>'LeaveController@getLeaveType']);
}

Employee routes are:

Route::group(['middleware' => ['auth','role:employee']], function(){
    Route::get('leave-type',['as'=>'leave.type','uses'=>'LeaveController@getLeaveType']);
}

Now when i login with admin i can't access leave-type route because admin user doesn't have employee role but when i assign admin user to employee role it will be accessible, and admin user can not be an employee so how can i accessible this route on both role.

Using laravel 5.4 and zizaco/entrust for ACL system. so please let me how what type of problem is this and how can get the solution.

Thanks in advance.

Upvotes: 0

Views: 2170

Answers (1)

Adrian Hernandez-Lopez
Adrian Hernandez-Lopez

Reputation: 553

If you need more routes that are available to a base group and then a specific subset for others I'd suggest to reorganize the routes file to something as follows:

Route::middleware(['auth'])->group(function () {

    //Routes available to all users
    Route::get('leave-type',['as'=>'leave.type', 'uses'=>'LeaveController@getLeaveType']);

    //Routes available to employees
    Route::middleware(['role:employee'])->group(function () {

    });

    //Routes available to Admin, HR Manager and Manager
    Route::middleware(['role:admin|hr-manager|manager'])->group(function () {
        Route::get('employee', ['as'=>'employee', 'uses'=>'EmployeeController@employeeList']); 
    });
});

Upvotes: 1

Related Questions