简体   繁体   English

Codeigniter RESTful API

[英]Codeigniter RESTful API

I have a mysql database with a table which contains several brands of cars. 我有一个带有一个表的mysql数据库,该表包含几个品牌的汽车。 I need to create an API for all, so right now I have something like this : 我需要为所有人创建一个API,所以现在我有了这样的东西:

<?php

require(APPPATH.'libraries/REST_Controller.php');

class carcollection extends REST_Controller{

// AUDI
public function audi_get()  
{ 
    $this->load->database();
    $sql = "SELECT car_id, car_brand, car_model FROM `cars` WHERE car_brand LIKE 'Audi' LIMIT 10";
    $query = $this->db->query($sql);
    $data = $query->result();

}

// BMW
public function bmw_get()  
{ 
    $this->load->database();
    $sql = "SELECT car_id, car_brand, car_model FROM `cars` WHERE car_brand LIKE 'BMW' LIMIT 10";
    $query = $this->db->query($sql);
    $data = $query->result();

}

...etc..etc.

This works fine so far, when I for example enter http://localhost/myprojects/cars/index.php/api/carcollection/audi my console/browser gives me the correct data in JSON format. 到目前为止,这工作正常,例如当我输入http://localhost/myprojects/cars/index.php/api/carcollection/audi我的控制台/浏览器会以JSON格式为我提供正确的数据。 When I enter http://localhost/myprojects/cars/index.php/api/carcollection/ - I get the error {"status":false,"error":"Unknown method."} - but I would like to know how I could do this so that I get all data for once? 当我输入http://localhost/myprojects/cars/index.php/api/carcollection/ -我收到错误{"status":false,"error":"Unknown method."} -但我想知道我该怎么做才能一次获得所有数据? with the URL shown before without basically changing the SQL queries... 使用之前显示的URL,而基本上不更改SQL查询...

Any help is appreciated... 任何帮助表示赞赏...

You need to include an index route: 您需要包括一个索引路由:

public function index_get() {
    $this->load->database();
    $sql = "SELECT car_id, car_brand, car_model FROM `cars` LIMIT 10";
    $query = $this->db->query($sql);
    $data = $query->result();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM