CodeIgniter - 在帖子中添加评论


CodeIgniter - adding comments to a post

我需要一些帮助。我有一个帖子页面,其中包含完整的帖子,在帖子下方有一个用于添加评论的小表单。帖子页面的 uri 是:site/posts/1,所以它在帖子控制器中,表单操作是form_open(site_url('comments/add/'.$post->post_id))

这是我在注释控制器中的add()函数:

public function add($post_id){
    // if nothing posted redirect
    if (!$this->input->post()) {
        redirect(site_url());
    }
    // TODO: save comment in database
    $result = $this->comment_model->add($post_id);
    if ($result !== false) {
        redirect('posts/'.$post_id);
    }
    // TODO:load the view if required
}

这是注释模型中的 add() 函数

public function add($post_id){
    $post_data = array(
        'post_id' => $post_id, 
        'username'  => $this->input->post('username'),
        'email'     => $this->input->post('email'),
        'comment'   => $this->input->post('comment')
    );
    if ($this->validate($post_data)) {
        $this->db->insert('comments', $post_data);
        if ($this->db->affected_rows()) {
            return $this->db->insert_id();
        }
        return false;
    } else {
        return false;
    }
}

我试图做的是,如果$result = $this->comment_model->add($post_id); 验证失败以在我的帖子视图中显示验证错误,否则插入评论并重定向到同一帖子页面(site/posts/1)。

问题是,当我点击提交时,表单操作会按预期进入评论/添加/1,但没有执行上述任何操作。

任何想法如何解决这个问题?

编辑我对代码进行了一个小的更改,没有"令人困惑"的 validate() 函数。也许这更有帮助。

注释控制器:

 public function add($post_id){
    // if nothing posted redirect
    if (!$this->input->post()) {
        redirect(site_url());
    }
    // TODO: save comment in database
    $this->form_validation->set_rules($this->comment_model->rules);
  if ($this->form_validation->run() == true) {
    echo "Ok! TODO save the comment.";
   // $this->comment_model->add($post_id);
   // redirect('posts/'.$post_id);
  } else {
      echo "Validation Failed! TODO: show validation errors!";
  }
    // TODO:load the view if required
}

评论模型:

 public function add($post_id){
    $post_data = array(
        'post_id' => $post_id, 
        'username'  => $this->input->post('username'),
        'email'     => $this->input->post('email'),
        'comment'   => $this->input->post('comment')
    );
        $this->db->insert('comments', $post_data);
        if ($this->db->affected_rows()) {
            return $this->db->insert_id();
        }
        return false;
} 
您需要

避免将validation_errors()传递回Posts控制器。目前,当您在add函数中执行重定向时(当验证失败时),您将丢失引发的验证错误。

我会考虑使用 flashdata (http://ellislab.com/codeigniter/user-guide/libraries/sessions.html) 将成功/错误消息从Comments控制器传递回Posts控制器。类似于以下内容:

注释控制器:

public function add($post_id) {
    // if nothing posted redirect
    if (!$this->input->post()) {
        redirect(site_url());
    }
    // TODO: save comment in database
    $this->form_validation->set_rules($this->comment_model->rules);
    if ($this->form_validation->run() == true) {
        // Store the success message in flash data
        $this->session->set_flashdata('message', 'Ok! TODO save the comment.');
        // Redirect back to posts page
        redirect('posts/'.$post_id, 'refresh');
    } else {
        // Store the error message in flash data
        $this->session->set_flashdata('message', validation_errors());
        // Redirect back to posts page
        redirect('posts/'.$post_id, 'refresh');
    }
}

帖子控制器:

public function index($post_id) {
    $this->data['message'] = $this->session->flashdata('message');
    $this->load->view('posts', $this->data);
}

帖子视图:

echo $message;

可能并不完美,但希望它有所帮助...