简体   繁体   中英

Codeigniter RESTful API

I have a mysql database with a table which contains several brands of cars. I need to create an API for all, so right now I have something like this :

<?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. 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? with the URL shown before without basically changing the SQL queries...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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