Laravel 7 - 嵌套资源路由中的范围问题

N'B*_*yev 8 php laravel laravel-7

路线:

我有一个这样的嵌套资源路由定义:

Route::resource('posts.comments', 'CommentController');
Run Code Online (Sandbox Code Playgroud)

这会产生以下路线:

+--------+-----------+--------------------------------------+------------------------+------------------------------------------------+------------+
| Domain | Method    | URI                                  | Name                   | Action                                         | Middleware |
+--------+-----------+--------------------------------------+------------------------+------------------------------------------------+------------+
|        | GET|HEAD  | posts/{post}/comments                | posts.comments.index   | App\Http\Controllers\CommentController@index   | web        |
|        | POST      | posts/{post}/comments                | posts.comments.store   | App\Http\Controllers\CommentController@store   | web        |
|        | GET|HEAD  | posts/{post}/comments/create         | posts.comments.create  | App\Http\Controllers\CommentController@create  | web        |
|        | GET|HEAD  | posts/{post}/comments/{comment}      | posts.comments.show    | App\Http\Controllers\CommentController@show    | web        |
|        | PUT|PATCH | posts/{post}/comments/{comment}      | posts.comments.update  | App\Http\Controllers\CommentController@update  | web        |
|        | DELETE    | posts/{post}/comments/{comment}      | posts.comments.destroy | App\Http\Controllers\CommentController@destroy | web        |
|        | GET|HEAD  | posts/{post}/comments/{comment}/edit | posts.comments.edit    | App\Http\Controllers\CommentController@edit    | web        |
+--------+-----------+--------------------------------------+------------------------+------------------------------------------------+------------+
Run Code Online (Sandbox Code Playgroud)

关系(在模型中):

Post 模型:

public function comments()
{
    return $this->hasMany(Comment::class);
}
Run Code Online (Sandbox Code Playgroud)

Comment 模型:

public function post()
{
    return $this->belongsTo(Post::class);
}
Run Code Online (Sandbox Code Playgroud)

虚拟数据(在表格中):

posts 桌子:

+----+--------+-----------------------------+---------------------+---------------------+
| id | title  | body                        | created_at          | updated_at          |
+----+--------+-----------------------------+---------------------+---------------------+
| 1  | Post 1 | This is the body of Post 1. | 2020-07-29 11:20:53 | 2020-07-29 11:20:53 |
| 2  | Post 2 | This is the body of Post 2. | 2020-07-29 11:21:13 | 2020-07-29 11:21:13 |
+----+--------+-----------------------------+---------------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

comments 桌子:

+----+---------+-----------------------------+---------------------+---------------------+
| id | post_id | body                        | created_at          | updated_at          |
+----+---------+-----------------------------+---------------------+---------------------+
| 1  | 1       | The comment for the Post 1. | 2020-07-29 11:22:27 | 2020-07-29 11:22:27 |
| 2  | 2       | The comment for the Post 2. | 2020-07-29 11:22:32 | 2020-07-29 11:22:32 |
+----+---------+-----------------------------+---------------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

文档中

当使用自定义键控隐式绑定作为嵌套路由参数时,Laravel 将自动将查询范围限定为通过其父级检索嵌套模型,使用约定来猜测父级上的关系名称。

所以,{comment}应该是 的孩子{post}。但是,当我打/posts/1/comments/2,它将检索注释与一个ID为2属于与一个ID为2。预期的结果是NotFoundHttpException

当我像这样单独定义路由时,它工作正常:

Route::get('/posts/{post}/comments/{comment:id}', 'CommentController@show');
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

还尝试自定义PostComment模型中的默认键名:

public function getRouteKeyName()
{
    return 'id';
}
Run Code Online (Sandbox Code Playgroud)

但没有运气。

任何帮助,将不胜感激。

N'B*_*yev 9

Illuminate\Routing\PendingResourceRegistration.php源代码阅读类后进行了一些挖掘并得出了结论。我必须使用自定义键控隐式绑定才能使其按预期工作。

Route::resource()方法采用(可选)第三个参数,它是一个关联数组。因此,我需要parameters使用此参数通过键覆盖路由参数名称。

Route::resource('posts.comments', 'CommentController', [
    'parameters' => ['comments' => 'comment:id'],
]);
Run Code Online (Sandbox Code Playgroud)

或者

Route::resource('posts.comments', 'CommentController')->parameters([
    'comments' => 'comment:id',
]);
Run Code Online (Sandbox Code Playgroud)

无论哪种方式都有效。