4

In laravel 4 i would like to have a nested controller.

I have read the documentation but didn't find any explanation on how to do it.

Supose that in a app i have some articles and each article have his own set of comments. I would like to be able to get all comment of a specific article by accessing a URL like this.

http://myapp.com/articles/5/comments

I have created a commentsController, but i don't know how to correctly get the article id from the url, so i can pass it to all my CRUD methods in my controller

2 Answers 2

5

in route.php

Route::resource('articles.comments','commentsController');

in controller

public function show($articleId, $comment) {}

public function create($articleId) {}
Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure nested resource controllers are the way to go.... Here is what I would do.

Route::resource('articles','articlesController');
Route::get('articles/{$id}/comments','articlesController@comments');

Then in your controller

public function comments($id) {

}

1 Comment

Although this would work, it would only provide a single route, removing the benefit of using a resource. Your suggested way you would need to manually add all the required GET, POST, PATCH & PUT routes..... messy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.