0

我正在使用 codeigniter REST API。在我的 API 调用中,我试图从中获取价值,$this->input->get('id')但没有从 get 中获得任何价值。

public function data_get($id_param = NULL){ 

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

    if($id===NULL){
        $id = $id_param;
    }
    if ($id === NULL)
    {
        $data = $this->Make_model->read($id);
        if ($data)
        {

            $this->response($data, REST_Controller::HTTP_OK); 
        }
        else
        {
            $this->response([
                'status' => FALSE,
                'error' => 'No record found'
            ], REST_Controller::HTTP_NOT_FOUND); 
        }
    }
    $data = $this->Make_model->read($id);
    if ($data)
    {
        $this->set_response($data, REST_Controller::HTTP_OK);   
    }
    else
    {
        $this->set_response([
            'status' => FALSE,
            'error' => 'Record could not be found'
        ], REST_Controller::HTTP_NOT_FOUND); 
    }
 }

在上面的代码中 $id 不返回任何值。

4

2 回答 2

0

请将您的代码从 更改$id = $this->input->get('id');$id = $this->get('id'); 这应该可以解决您的问题。

于 2018-06-13T10:41:44.443 回答
0

希望对你有帮助 :

使用其中一个$this->input->get('id')$this->get('id')两个都应该工作

你的data_get方法应该是这样的:

public function data_get($id_param = NULL)
{ 

    $id = ! empty($id_param) ? $id_param : $this->input->get('id');
    /* 
     u can also use this
     $id = ! empty($id_param) ? $id_param : $this->get('id');
    */
    if ($id)
    {
        $data = $this->Make_model->read($id);
        if ($data)
        {

            $this->response($data, REST_Controller::HTTP_OK); 
        }
        else
        {
            $this->response([
                'status' => FALSE,
                'error' => 'No record found'
            ], REST_Controller::HTTP_NOT_FOUND); 
        }
    }
    else
    {
        $this->response([
            'status' => FALSE,
            'error' => 'No id is found'
        ], REST_Controller::HTTP_NOT_FOUND); 
    }
}
于 2018-06-13T11:19:35.113 回答