4

In Laravel 4, I wish to create a set of restful resources as follows:

http://localhost/posts/1/comments   
http://localhost/posts/1/comments/1 
http://localhost/posts/1/comments/1/edit

...
So I created two controllers: PostsController and CommentsController (on the same layer), and the routes are written as below:

Route::resource('posts', 'PostsController');

Route::resource('posts.comments', 'CommentsController');

I also created a link in /views/comments/index.blade.php referring to routes: posts.comments.create

{{ link_to_route('posts.comments.create', 'Add new comment') }}

Here's the problem I met:

When I visit http://localhost/posts/1/comments, the page throws MissingMandatoryParametersException, indicating:

Some mandatory parameters are missing ("posts") to generate a URL for route "posts.comments.create".

How can I fix the problem, and how can I know whether the solution also applies for the create and edit methods in CommentsController?

e.g.

 public function index()
{
    $tasks = $this->comment->all();

    return View::make('comments.index', compact('comments'));

}

public function create()
    {
       return View::make('comments.create');

    }

public function show($post_id,$comment_id)  
    {  
        $comment = $this->comment->findOrFail($comment_id);  

        return View::make('comments.show', compact('comment'));  

    }
Expedito
  • 7,771
  • 5
  • 30
  • 43
JackpotK
  • 313
  • 1
  • 8
  • 18

1 Answers1

7

I'm using nested controllers in two projects, love them. The problem seems to be in your controller and route link.

In the CommentsController, the $post_id is missing. Do something like this:

public function create($post_id)
{
   return View::make('comments.create')
    ->with('post_id', $post_id);
}

When creating links to a nested controller, the ID's of all of the ancestors must be provided. In this case the $post_id is again missing. You may have to make it available to your view if it's not already.

{{ HTML::linkRoute('posts.comments.create', 'Add new comment', $post_id) }}
Codeplus
  • 131
  • 5
  • Thank you! I got another issues when writing a redirect::route as below: `return Redirect::route('posts.comments.create')->with('post_id',$post_id) ->withInput() ->withErrors($validation) ->with('message', 'There were validation errors.');` It still throw the same exception,anything wrong? – JackpotK May 20 '13 at 14:25