REST API with CodeIgniter

11,794

This is old question but if somebody go here and still need help, try these code:

In your controller:

public function index_get()  
{  
    $where = '';  

    if ($this->get('id'))  // search by id when id passed by
    {              
        $where .= 'id = '.$this->get('id');  
    }  

    if ($this->get('name'))  // add search by name when name passed by        
    {  
        $where .= (empty($where)? '' : ' or ')."name like '%".$this->get('name')."%'";  
    }  

    if ($this->get('city'))  // add search by city when city passed by  
    {  
        $where .= (empty($where)? '' : ' or ')."city like '%".$this->get('city')."%'";  
    }  

    // you can add as many as search terms

   if ( ! empty($where))  
   {  
       $data = $this->grid_m->get_searched($where);  

       if ($data)  
       {  
           $this->response($data, 200);  
       }  
       else  
       {  
           $this->response(NULL, 404);  
       }  
    }  
    else  
    {  
        $this->response(NULL, 404);  
     }  
}

In your model, create get_searched function:

class Grid_m extends CI_Model  
{  
   function get_searched($where = NULL)  
   {  
      if ( ! is_null($where))  
      {  
          $this->db->where($where);  
          $query = $this->db->get('grid');  

          if ($query->num_rows() > 0)  
          {  
              return $query->result();  
          }  
       }  
       return FALSE;  
    }  
}  
Share:
11,794
Brian
Author by

Brian

Lead Software Engineer at PetSmart with the Stores & Services Engineering Team. I specialize in front-end application architecture development and native mobile application development with Swift, with the occasional dabbling in React Native.

Updated on June 25, 2022

Comments

  • Brian
    Brian almost 2 years

    I am creating a web-service backend for a mobile app I am developing. (I am an experience Obj-C developer, not a web-designer!) Essentially I would like to use Codeigniter and Phil Sturgeon's RESTful API Server https://github.com/philsturgeon/codeigniter-restserver however, I'm having some trouble getting it all setup and working.

    I have MySQL database that is setup with data in it. And I need some help writing a CodeIgniter PHP model and controller that returns JSON data of what is inside that database. All of the tutorials and forum post's i have found deal with hardcoded data in the controler, not a MySQL database. I would ideally like to have the URL formatted like this http://api.mysite.com/v1/search?id=1&name=foo&city=bar , I could potentially have 50+ parameters to pass in the url.

    Using Phil's code, I have come up with this as my controller:

    public function index_get()
    {
        if (!$this->get('id'))
        {
            $this->response(NULL, 400);
        }
    
        $data = $this->grid_m->get_id($this->get('id'));
        if ($data)
        {
            $this->response($data, 200);
        }
        else
        {
            $this->response(NULL, 404);
        }
    }
    

    That only gets me one search term: id?=# .. I need to know how to get multiple search terms

    Here is my Codeigniter model:

    <?php
    
    class Grid_m extends CI_Model
    {
    
    function get_all()
    {
        $query = $this->db->get('grid');
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
        return FALSE;;
    }
    

    This just returns EVERYTHING in my MySQL database regardless of what id or url term I pass it in the URL.

    I'm a big noob when it comes to developing my own custom API so any suggestions on how to fix my controller and database model would be a huge help!

    Thanks for the help!

    -brian