如何在Laravel 4上路由子资源?

Mau*_*uro 3 php laravel laravel-4

所以我写了一个嵌套资源的简单应用程序:帖子和评论.当然,帖子是父母,评论是孩子,每个一对一相关.很简单.现在我想要评论索引(和创建,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');
});
Run Code Online (Sandbox Code Playgroud)

但由于路由名称已注册为,因此无法正常工作posts.{post_id}.comments.create.基本上post_id占位符被计为路线的一部分,并不是很整洁.任何方式做得很好还是我应该逐个编写路由并摆脱group/Route :: resource/prefix的事情?

Bur*_*dem 8

您可以使用这样的嵌套资源;

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

当你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         |                |               |
+--------+-------------------------------------------------+------------------------+------------------------------------+----------------+---------------+
Run Code Online (Sandbox Code Playgroud)