Wednesday, 7 August 2013

Nesting shared subresource in Laravel

Nesting shared subresource in Laravel

I am wondering how to differentiate between requests from different parent
resources in a subresource.
Consider the subresource comments, which is shared both by posts and
users. How does the comments controller know what the correct parent
resource is?
I got these routes:
Route::resource('posts', 'PostsController');
Route::resource('posts.comments', 'CommentsController');
Route::resource('users', 'UsersController');
Route::resource('users.comments', 'CommentsController');
In CommentsController, I got this function:
public function index($parent_id) {
// return multiple comments here
}
So I may want to display all the comments belonging to a post or all the
comments belonging to a user, but how can the CommentsController tell if
$parent_id refers to a post or a user in this case?

No comments:

Post a Comment