我試圖從我的MySQL數據庫中提取數據,所以我可以創建一個API,但我不斷收到錯誤的請求。我的Db有一個包含ID,名稱和狀態的表「category」。Codeigniter RESTful API - HTTP/1.1 400錯誤請求
我的控制器(category.php - I創建了 「控制器」 -folder,其中category.php被置於內部的 「API」 文件夾中)
<?php
require(APPPATH.'libraries/REST_Controller.php');
class Category extends REST_Controller{
public function index_get()
{
//IF CATEGORY NOT EXIST
if(!$this->get('name'))
{
$this->response(NULL, 400);
}
//CHECK TO MODEL FUNCTION
$category = $this->category_model->get_category($this->get('name'));
//IF CATEGORY EXIST
if($category)
{
$this->response($category, 200); // 200 being the HTTP response code
}
}
public function index_put()
{
//
}
public function index_post()
{
//
}
public function index_delete()
{
//
}
}
?>
和我的模型(category_model.php)
<?php
class Category_model extends CI_Model{
function __construct() {
parent::__construct();
}
function get_category($name){
$query = $this->db->get('category', array('name'=>$name));
return $query->row_array();
}
}
?>
現在,當我進入http://localhost/projectfolder/ci/index.php/api/category/
- 瀏覽器顯示什麼,同樣做控制檯...
問題是什麼?
嘗試此:'http:// localhost/projectfolder/ci/index.php/api/category/index_get' – user2936213
@ user2936213這給出我'{「狀態」:錯誤,「錯誤」:「未知的方法。」} - 但我必須輸入_get? – SHT