简体   繁体   English

Codeigniter Rest Api使用参数ID删除

[英]Codeigniter Rest Api Delete using parameter ID

I'm Learning CRUD operation with Codeigniter and Rest Api for my app Flutter .But I'm get some problem with Delete operation. 我正在为我的应用Flutter学习Codeigniter和Rest Api的CRUD操作,但是Delete操作却遇到了一些问题。 I'm want delete data using parameter ID , but the message show me ID Null. 我想使用参数ID删除数据,但是消息显示ID为Null。

It's my Get Operation using parameter 这是我使用参数获取操作

http://192.168.43.159/wpu-rest-server/apii/mahasiswa/get?id=5 

Possible i'm make delete operation like this ? 可能我正在像这样进行删除操作?

http://192.168.43.159/wpu-rest-server/apii/mahasiswa/delete?id=5 

I already try 我已经尝试了

http://192.168.43.159/wpu-rest-server/apii/mahasiswa/delete/5
http://192.168.43.159/wpu-rest-server/apii/mahasiswa/delete/id/5

But Nothing change , still ID null. 但没有任何变化,ID仍为null。
It's my Controller And Model Rest Api : 这是我的Controller and Model Rest Api:
Controller 控制者

public function delete_delete()
    {
        $id = $this->delete('id');
        $msgDelete = ['id' => $id, 'message' => 'Deleted the resource'];
        $msgEmpty = ['status' => false, 'message' => 'ID Not Found'];
        $msgBadRequest = ['status' => false, 'message' => 'Provide an ID'];

        if ($id === null) {
            $this->set_response($msgBadRequest, 400);
        } else {
            if ($this->mahasiswa->deleteMahasiswa($id) > 0) {
                $this->set_response($msgDelete, 204);
            } else {
                $this->set_response($msgEmpty, 404);
            }
        }
    }

Model 模型

public function deleteMahasiswa($id)
    {
        $this->db->delete('mahasiswa', ['id' => $id]);
        return $this->db->affected_rows();
    }

I am seeing some ambigius things in your code for example the api url is : 我在您的代码中看到了一些歧义词,例如api网址是:

http://192.168.43.159/wpu-rest-server/apii/mahasiswa/delete/5

and your controller method name is delete_delete . 并且您的控制器方法名称为delete_delete。

If you are using the get method you should respect the following : 如果使用的是get方法,则应注意以下几点:

base_url/controller_name/method_name?id=3

and then you can get the value with : 然后您可以使用以下方法获取值:

$this->input->get('id')

otherwise if you are passing the id as a method argument like the following : 否则,如果您将id作为方法参数传递,如下所示:

http://your_domain/controller_name/delete/5

you can get the value like this : 您可以得到像这样的值:

class controller_name extends CI_Controller {
.
.
.
public function delete($id=null){

}


}

If Possible do like this 如果可能的话,这样做

192.168.43.159/wpu-rest-server/apii/mahasiswa/delete_delete?id=5

Controller 控制者

public function delete_delete() {
 $id = $this->input->get('id');
 $this->mahasiswa->deleteMahasiswa($id);
 redirect('http://192.168.43.159/wpu-rest-server/apii/mahasiswa'); 
}

Model 模型

function deleteMahasiswa() {
 $this->db->where('id', $id);
 $this->db->delete('mahasiswa'); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM