Bri*_*ian 6 php api codeigniter-2
我正在为我正在开发的移动应用程序创建一个Web服务后端.(我是Obj-C开发人员,不是网页设计师!)基本上我想使用Codeigniter和Phil Sturgeon的RESTful API服务器https://github.com/philsturgeon/codeigniter-restserver但是,我有一些无法完成所有设置和工作.
我有MySQL数据库,其中设置了数据.我需要一些帮助来编写CodeIgniter PHP模型和控制器,它返回该数据库内部的JSON数据.我发现所有的教程和论坛帖子都是在控制器中处理硬编码数据,而不是MySQL数据库.理想情况下,我希望网址格式为http://api.mysite.com/v1/search?id=1&name=foo&city=bar,我可能会有50多个参数传递到网址中.
使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
这只能得到一个搜索词: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;;
}
Run Code Online (Sandbox Code Playgroud)
这只是在我的MySQL数据库中返回一切,无论我在URL中传递了什么id或url术语.
在开发我自己的自定义API时,我是一个很大的菜鸟,因此任何关于如何修复我的控制器和数据库模型的建议都将是一个巨大的帮助!
谢谢您的帮助!
布赖恩
用户使用Codeigniter的活动记录来构建适当的查询,您可以使用活动记录的方法构建任何类型的查询,请参阅以下示例,我刚刚在其中添加了一个条件,您可以根据需要添加更多条件。
<?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;;
}
}
Run Code Online (Sandbox Code Playgroud)