php-Codeigniter Rest API简单CRUD

时间:2018-07-19 06:55:45

标签: php rest codeigniter codeigniter-3 codeigniter-restapi

在将数据插入需要使用POSTMAN的POST方法的MySQL时,我遇到了这个问题。

此功能data_post()从数据库中插入数据,但是当我尝试使用邮递员插入数据原始数据时

{"id":"2","name":"ropen","password":"pamela005"}

邮递员出现此错误:

  

405不允许的方法

这是我的控制人 Users.php

public function data_post(){
    $params = [
        'id' => 1,
        'name' => 'John Doe',
        'password' => 'test'
    ];
    $resp = $this->user_model->data($params);
    $this->set_response($resp, REST_Controller::HTTP_CREATED);    
}

模型 User_model.php

public function data($data){   
      $this->db->insert('user',$data);
   }

1 个答案:

答案 0 :(得分:0)

希望这对您有帮助:

您必须首先使用post获取$this->post()数据,应该是这样的:

注意:如果您的id列是自动递增的,则无需在$params

中添加ID
public function data_post()
{
    $id = $this->post('id');
    $name = $this->post('name');
    $password = $this->post('password');

    $params = array('id' => $id,'name' => $name,'password' => $password);
    $resp = $this->user_model->data($params);
    $this->set_response($resp, REST_Controller::HTTP_CREATED);    
} 

更多信息:https://github.com/chriskacerguis/codeigniter-restserver#handling-requests