因此,我编写了一个具有嵌套资源的简单应用程序:帖子和评论。自然,帖子是父母,评论是孩子,每个帖子都是一对一的。很简单。现在,我希望注释索引(和创建,POST)页面位于posts/{post_id}/comments
,更新(PUT)位于/posts/{post_id}/comments/{comment_id}
。所以我尝试了这个:
Route::resource('posts', 'PostsController');
Route::group(array('prefix' => 'posts/{post_id}'), function() {
Route::resource('comments', 'CommentsController');
});
但由于路由名称已注册为,因此无法使用posts.{post_id}.comments.create
。基本上,post_id占位符被视为路由的一部分,并且不是整齐的。有什么好的方法吗?还是我应该一个一个地写路由并摆脱组/ Route :: resource / prefix的问题?
您可以像这样使用嵌套的资源。
Route::resource('posts', 'PostsController');
Route::resource('posts.comments', 'CommentsController');
当您php artisan routes
看到新路线时;
+--------+-------------------------------------------------+------------------------+------------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+-------------------------------------------------+------------------------+------------------------------------+----------------+---------------+
| | GET|HEAD posts | posts.index | PostsController@index | | |
| | GET|HEAD posts/create | posts.create | PostsController@create | | |
| | POST posts | posts.store | PostsController@store | | |
| | GET|HEAD posts/{posts} | posts.show | PostsController@show | | |
| | GET|HEAD posts/{posts}/edit | posts.edit | PostsController@edit | | |
| | PUT posts/{posts} | posts.update | PostsController@update | | |
| | PATCH posts/{posts} | | PostsController@update | | |
| | DELETE posts/{posts} | posts.destroy | PostsController@destroy | | |
| | GET|HEAD posts/{posts}/comments | posts.comments.index | CommentsController@index | | |
| | GET|HEAD posts/{posts}/comments/create | posts.comments.create | CommentsController@create | | |
| | POST posts/{posts}/comments | posts.comments.store | CommentsController@store | | |
| | GET|HEAD posts/{posts}/comments/{comments} | posts.comments.show | CommentsController@show | | |
| | GET|HEAD posts/{posts}/comments/{comments}/edit | posts.comments.edit | CommentsController@edit | | |
| | PUT posts/{posts}/comments/{comments} | posts.comments.update | CommentsController@update | | |
| | PATCH posts/{posts}/comments/{comments} | | CommentsController@update | | |
| | DELETE posts/{posts}/comments/{comments} | posts.comments.destroy | CommentsController@destroy | | |
+--------+-------------------------------------------------+------------------------+------------------------------------+----------------+---------------+
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句