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

时间:2014-05-09 21:29:54

标签: 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');
});

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

1 个答案:

答案 0 :(得分:8)

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

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         |                |               |
+--------+-------------------------------------------------+------------------------+------------------------------------+----------------+---------------+