Laravel 4:Route :: post返回URL,其中模型名称用括号代替id

时间:2014-05-23 13:00:27

标签: laravel-4

我正在构建一个帖子/评论系统,并在帖子视图中添加评论表单。因此,当我在网址http://example.dev/post/1中观看帖子并点击表单提交时,网址会转到http://example.dev/post/%7Bpost%7D,其中%7B = {和{{ 1}} = %7D)。

我认为与url post方法相关联的控制器甚至无法启动。

我的路线:

}

我的viewPost控制器:

Route::model('post','Post');

Route::get('partido/{post}', 'FrontendController@viewPost');

Route::post('partido/{post}', array(
    'before' => 'basicAuth',
    'uses' => 'FrontendController@handleComment'
    )
);

我的handleComment控制器:

public function viewPost(Post $post)
{   
    $comments = $post->comments()->get();

    return View::make('post')
                    ->with(compact('comments'))
                    ->with(compact('posts'));
}

视图中的表单:

public function handleComment(Post $post)
    {
        // Get the data
        $data = Input::all();
        // Build the rules
        $rules = array(
            'title' => 'required',
            'description' => 'required',
        );
        // Error messages
        $messages = array(
            'title.required' => 'Title required.',
            'description.required' => 'Description required.',
        );

        // Validator: He Comes, He sees, He decides
        $validator = Validator::make($data, $rules, $messages);

        if ($validator->passes()) {
            // Save the new comment.
           $comment = new Comment;
           $comment->title = Input::get('title');
           $comment->description = Input::get('description');
           $post->comments()->save($comment);

           return Redirect::to('post/'.$post->id.'');
        }
       else {
           return Redirect::to('post/'.$post->id.'')->withErrors($validator);
       }
    }

1 个答案:

答案 0 :(得分:3)

Form::open()需要另一个数组 - 试试这个:

{{ Form::open(array('action' => array('FrontendController@handleComment', $post->id))) }}