简体   繁体   中英

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. I'm want delete data using parameter ID , but the message show me 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.
It's my 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 :

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

and your controller method name is delete_delete .

If you are using the get method you should respect the following :

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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