Reputation: 3857
I have 2 roles: admin and agent, and I have a search page. I want to allow 2 roles to access this page.
So if an admin logs in then it can access the search page, and if an agent logs in then it can access the search page as well.
In the route.php
file I am doing the following:
Route::group(['middleware' => 'admin'], function(){
Route::post('customer/search', 'CustomersController@search');
});
Route::group(['middleware' => 'agent'], function(){
Route::post('customer/search', 'CustomersController@search');
});
In this case an admin can not access the search page.
And if reverse the middleware like this:
Route::group(['middleware' => 'agent'], function(){
Route::post('customer/search', 'CustomersController@search');
});
Route::group(['middleware' => 'admin'], function(){
Route::post('customer/search', 'CustomersController@search');
});
the agent can not access the search page.
So, how can I allow the admin and agent roles to both access the search page.
Upvotes: 1
Views: 167
Reputation: 44526
If each middleware checks its own role (if the user is an admin
and respectively an agent
), then the first executed middleware will always restrict access to the last in one of the cases. There's no way around that if you do a separate check in each middleware. You perhaps need a common middleware that validates all roles that are allowed to access the search route. So something like this:
Route::group(['middleware' => 'searchRoles'], function()
{
Route::post('customer/search', 'CustomersController@search');
});
Upvotes: 2