library input class not working in php codeigniter

286 Views Asked by At

I'm working with codeigniter REST API. In my API call i'm trying to get value from $this->input->get('id') but does not get any value from the 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); 
    }
 }

In the above code $id doesn't return any value.

2

There are 2 best solutions below

0
Pradeep On BEST ANSWER

Hope this will help you :

Use either $this->input->get('id') or $this->get('id') both should work

Your data_get method should be like this :

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); 
    }
}
1
Virendra Jadeja On

Please change your code from $id = $this->input->get('id'); to $id = $this->get('id'); This should solve your problem.