2013-05-19 2 views
4

, 나는 다음과 같이 편안 자원 세트를 만들려 : Laravel 4 : 중첩 된 리소스에 대해 올바른 중첩 컨트롤러를 작성하는 방법? Laravel 4에서

http://localhost/posts/1/comments 
http://localhost/posts/1/comments/1 
http://localhost/posts/1/comments/1/edit 
은 ...
그래서 나는 두 개의 컨트롤러 생성 : (같은 층) 가 PostsControllerCommentsController을, 그리고 경로는 다음과 같이 기록됩니다 posts.comments.create

0 : 나는 또한 경로를 참조 /views/comments/index.blade.php에 링크를 생성

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

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

을 일부 필수 매개 변수가 누락 ("게시물")에 대한 URL을 생성 : 나는 http://localhost/posts/1/comments 방문 할 때

이 페이지가 표시, MissingMandatoryParametersException를 던졌습니다 :

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

여기에 내가 만난 문제입니다 "posts.comments.create"를 라우팅하십시오.

어떻게 해결할 수 있습니까? 솔루션이 CommentsController의 작성 및 편집 방법에도 적용되는지 여부를 어떻게 알 수 있습니까?

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')); 

    } 

답변

7

두 개의 프로젝트에서 중첩 된 컨트롤러를 사용하고 있습니다. 문제가 귀하의 컨트롤러 및 경로 링크에있는 것 같습니다.

CommentsController에서 $ post_id가 누락되었습니다.

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

중첩 된 컨트롤러에 대한 링크를 만들 때 모든 조상의 ID를 제공해야합니다. 이 경우 $ post_id가 다시 누락됩니다. 아직보기가 아니라면보기에 사용 가능하게 설정해야 할 수도 있습니다.

{{ HTML::linkRoute('posts.comments.create', 'Add new comment', $post_id) }} 
+0

감사합니다. redirect :: route를 작성할 때 또 다른 문제가 있습니다 :'redirect :: route ('posts.comments.create') -> ('post_id', $ post_id) -> withInput() -> withErrors ($ validation) -> ('message', '유효성 검사 오류가 있습니다.')'여전히 동일한 예외가 발생합니다. – JackpotK

관련 문제