【问题标题】:REST API with CodeIgniter带有 CodeIgniter 的 REST API
【发布时间】:2012-11-13 00:14:04
【问题描述】:

我正在为我正在开发的移动应用程序创建 Web 服务后端。 (我是经验丰富的 Obj-C 开发人员,而不是网页设计师!)本质上,我想使用 Codeigniter 和 Phil Sturgeon 的 RESTful API 服务器https://github.com/philsturgeon/codeigniter-restserver,但是,我在设置和工作时遇到了一些麻烦。

我有 MySQL 数据库,其中设置了数据。我需要一些帮助来编写 CodeIgniter PHP 模型和控制器,以返回该数据库中内容的 JSON 数据。我发现的所有教程和论坛帖子都处理控制器中的硬编码数据,而不是 MySQL 数据库。理想情况下,我希望 URL 格式像这样 http://api.mysite.com/v1/search?id=1&name=foo&city=bar ,我可能有 50 多个参数来传递 url。

使用 Phil 的代码,我想出了这个作为我的控制器:

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

这只会给我一个搜索词:id?=# .. 我需要知道如何获取多个搜索词

这是我的 Codeigniter 模型:

<?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;;
}

无论我在 URL 中传递什么 id 或 url 术语,这只会返回我的 MySQL 数据库中的所有内容。

在开发自己的自定义 API 方面,我是个大菜鸟,所以任何关于如何修复我的控制器和数据库模型的建议都会有很大帮助!

感谢您的帮助!

-布莱恩

【问题讨论】:

  • 好吧,你的方法被称为“get_all”,所以我认为行为是正确的......改为显示“get_id()”方法

标签: php api codeigniter-2


【解决方案1】:

用户Codeigniter's Active record 构建正确的查询,您可以使用活动记录的方法构建任何类型的查询,参考以下示例,我刚刚在其中添加了一个条件,您可以根据需要添加更多条件。

 <?php
class Grid_m extends CI_Model
{    
  function get_all()
  {
     $this->db->select('col1, col2, col3')->where('id', 5);
     $query = $this->db->get('grid');

    if ($query->num_rows() > 0)
    {
        return $query->result();
    }
    return FALSE;;
  }

}

【讨论】:

【解决方案2】:

请检查您的查询

$query = $this->db->get_where('grid', array('id' => $id));

【讨论】:

    【解决方案3】:

    这是一个老问题,但如果有人去这里仍然需要帮助,请尝试以下代码:

    在你的控制器中:

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

    在你的模型中,创建 get_searched 函数:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多