Laravel 8 浅层嵌套
通常,并不完全需要在 URI 中同时拥有父 ID 和子 ID ,因为子 ID 已经是唯一的标识符。当使用唯一标识符(如自动递增的主键)来标识 URI 中的模型时,可以选择使用「浅嵌套」的方式定义路由:
Route::resource('photos.comments', CommentController::class)->shallow();
上面的路由定义方式会定义以下路由:
| HTTP 方式 | URI | 行为 | 路由名称 |
|---|---|---|---|
| GET | /photos/{photo}/comments
|
index | photos.comments.index |
| GET | /photos/{photo}/comments/create
|
create | photos.comments.create |
| POST | /photos/{photo}/comments
|
store | photos.comments.store |
| GET | /comments/{comment}
|
show | comments.show |
| GET | /comments/{comment}/edit
|
edit | comments.edit |
| PUT/PATCH | /comments/{comment}
|
update | comments.update |
| DELETE | /comments/{comment}
|
destroy | comments.destroy |