如何更改 Laravel 中嵌套资源路由的名称?

How to change name of Nested Resource Route in Laravel?

我的问题是关于何时像这样更改资源名称:

Route::resource('photos', 'Photos\PhotoController')->parameters(['photo' => 'photo_id']);

有效,默认 "photo" 参数名称更改为 "photo_id"。但是当我像这样使用嵌套资源路由时:

Route::resource('photos.captions', 'Photos\PhotoController')->parameters(['photo' => 'photo_id', 'caption' => 'caption_id']);

"caption" 参数名称未更改为 "caption_id"。

有什么办法可以同时改变它们吗? 谢谢:)

以下应该有效:

Route::resource('photos.captions', 'Photos\PhotoController')
    ->parameters(['photos' => 'photo_id', 'captions' => 'caption_id']);

资源名称和参数名称必须匹配:

  • 资源:photos,参数:photos
  • 资源:captions,参数:captions

来自docs

By default, Route::resource will create the route parameters for your resource routes based on the "singularized" version of the resource name. You can easily override this on a per resource basis by using the parameters method. The array passed into the parameters method should be an associative array of resource names and parameter names:

Route::resource('users', 'AdminUserController')->parameters([
    'users' => 'admin_user'
]);

The example above generates the following URIs for the resource's show route:

/users/{admin_user}