Reputation: 1034
I want to use a different (plural) model name for my route names, because my url is in a different language.
I can Achieve it with Restful Naming Resource Routes
Route::resource('/foo', BarController::class)->parameters([
'foo' => 'bar',
])->names([
'index' => 'bar.index',
'create' => 'bar.create',
'store' => 'bar.store',
'show' => 'bar.show',
'edit' => 'bar.edit',
'update' => 'bar.update',
'destroy' => 'bar.destroy',
]);
But.. I would have to do it for every resource route:
Route::resource('/usuarios', UserController::class)->parameters([
'usuarios' => 'user',
])->names([
'index' => 'user.index',
'create' => 'user.create',
'store' => 'user.store',
'show' => 'user.show',
'edit' => 'user.edit',
'update' => 'user.update',
'destroy' => 'user.destroy',
]);
How could I make this DRY?
Upvotes: 2
Views: 1266
Reputation: 3411
You can extract that part into a function.
function routeNames($model)
{
return array_map(
fn ($n) => "{$model}.{$n}",
['index', 'create', 'store', 'show', 'edit', 'update', 'destroy']
);
}
Route::resource('/usuarios', UserController::class)->parameters([
'usuarios' => 'user',
])->names(routeNames('user'));
Upvotes: 1