I am working on some legacy code and I feel I can convert all the different route groups to one.
This is the situation now:
There is page that shows a link of links to some data tables.
each link goes to the table's URL:
example.com/tables/table1
example.com/tables/table2
example.com/tables/table3
Each page of the tables has its own group of routes for the show and other actions, but is also surrounded by role Middleware, because each table can be accessed by different groups of users:
Route::prefix('tables')->group(function () {
// table_1
Route::middleware(['role:role_x'])->group(function () {
Route::get('/table1', [TablesController::class, 'table1_show'])->name('name1_1')
Route::patch('/table1', [TablesController::class, 'table1_patch'])->name('name1_2')
Route::post('/table1', [TablesController::class, 'table1_post'])->name('name1_3')
Route::get('/table1/sub_route', [TablesController::class, 'table1_sub_show'])->name('name1_4')
});
// table_2
Route::middleware(['role:role_y'])->group(function () {
Route::get('/table2', [TablesController::class, 'table2_show'])->name('name2_1')
Route::patch('/table2', [TablesController::class, 'table2_patch'])->name('name2_2')
Route::post('/table2', [TablesController::class, 'table2_post'])->name('name2_3')
Route::get('/table2/sub_route', [TablesController::class, 'table2_sub_show'])->name('name2_4')
});
// and many more table route groups
});
I was thinking about converting all these similar groups to one, something like that:
Route::prefix('tables')->group(function () {
Route::get('/{table_name}',
Route::patch('/{table_name}',
Route::post('/{table_name}',
Route::get('/{table_name}/sub_route',
});
And move the role Middleware to either the Controller or the Form Request authorization part.
But I am not sure that it's the correct way/syntax. Also currently each route explicitly points to specific Controller action and has its own name, because there are a few different things in the code currently, for example, each controller method uses the corresponding Model that belongs to the table name in the Controller. For that I was thinking to use the endpoint to fetch the corresponding Model / table that we take the data from instead of explicitly doing so in separate Controller action for each table.
Is it a good idea? Or there are some things that might not work this way?