API REST 不工作路由

API REST don't work routes

我正在使用 codeigniter,与提供官方网站的库一起 api 休息一下。

问题是:文件 routes.php 没有很好地重定向。当我将 localhost/API/1 放入浏览器时出现 404 错误。

这是我的控制器 "Apicontroller":

public function __construct() { //constructor //no tocar
    parent::__construct();
    $this -> load -> model("Modelocontrolador");
}

public function index_get() { //get all the info
    $datos_devueltos = $this->Modelocontrolador->getPrueba(NULL, "Usuarios"); 

    if(!is_null($datos_devueltos)){
       $this->response(array("response" => $datos_devueltos), 200);
    }else{
       $this->response(array("response" => "No date"), 200); 
    }
}
public function  find_get($id){ //select where
    $datos_devueltos = $this->Modelocontrolador->getPrueba($id, "Usuarios");
    if($id != NULL){
        if(!is_null($datos_devueltos)){
           $this->response(array("response" => $datos_devueltos), 200);
        }else{
           $this->response(array("response" => "No date"), 200); 
        }
    }else{
        $this->response(array("response" => "No dates for search"), 200); 
    }
}

public function index_post() { //insert in la table
    if(! $this -> post("dato")){
        $this->response(array("response" => "No enought info"), 200); 
    }else{
        $datoID = $this -> Modelocontrolador -> save($this -> post("dato"),"UsuariosJJ");

        if(!is_null($datoID)){
           $this->response(array("response" => $datoID), 200); 
        }else{
           $this->response(array("response" => "No found it"), 200); 
        }
    }
}
public function index_put($id) { //"update"
    if(! $this -> post("dato") || ! $id){
        $this->response(array("response" => "No ha mandado informacion correcta para el update"), 200); 
    }else{
        $datoID = $this -> Modelocontrolador -> update("Uid",$id,$this -> post("dato"),"UsuariosJJ");

        if(!is_null($datoID)){
           $this->response(array("response" => "Dato actualizado"), 200); 
        }else{
           $this->response(array("response" => "Error modify"), 200); 
        }
    }

}
public function index_delete($id) {
    if(! $id){
        $this->response(array("response" => "Not enought info"), 200); 
    }else{
        $delete = $this-> Modelocontrolador -> delete("Uid",$id,"UsuariosJJ");
    }

    if(!is_null($delete)){
        $this->response(array("response" => "Date delete"), 200); 
    }else{
        $this->response(array("response" => "Error delete"), 200); 
    }

}}

还有我的路线文件:

$route['default_controller'] = 'Apicontroller';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

/*sub-rutas*/
/*---------*/
$route["Apicontroller"]["get"] = "Apicontroller/index"; //basico
$route["Apicontroller/(:num)"]["get"] = "Apicontroller/find"; //select
$route["Apicontroller"]["post"] = "Apicontroller/index"; //insert
$route["Apicontroller/(:num)"]["put"] = "Apicontroller/index/"; //update
$route["Apicontroller/(:num)"]["delete"] = "Apicontroller/index/"; //delete

如果浏览器请求字面上使用 /API,那么路由需要 'see'。此外,路由规则必须明确显示要调用的方法。 (希望显示的代码反映了您心中的映射。)

/*sub-rutas*/
/*---------*/
$route["API"]["get"] = "Apicontroller/index_get"; //basico
$route["API/(:num)"]["get"] = "Apicontroller/find_get/"; //select
$route["API"]["post"] = "Apicontroller/index_post"; //insert
$route["API/(:num)"]["put"] = "Apicontroller/index_put/"; //update
$route["API/(:num)"]["delete"] = "Apicontroller/index_delete/"; //delete

使用上面的路由我创建了一些测试代码。这是那些文件。

大大简化的 Apicontroller。

class Apicontroller extends CI_Controller
{
  function __construct()
  {
    parent::__construct();
  }

  function index_get()
  {
    echo "API index";
  }

  public function find_get($id)
  { //select where
    echo "API find_get $id";
  }

  public function index_post()
  {
    echo 'API index_post';
  }

  public function index_put($id)
  { //"update"
    echo "API put $id";
  }
}

我不相信因为你的 Apicontroller 正在扩展一个不同的 Class 结果会改变。这可能是一个极端的假设。

为了测试 POST 调用,我使用了这两个文件。 首先是 Testpost.php 控制器

class Testpost extends CI_Controller
{
  public function __construct()
  {
    parent::__construct();
    $this->load->helper('form');
  }

  public function index()
  {
    $this->load->view("test");
  }

}

上面加载的非常简单的视图(test.php)。

<?php
echo form_open("API");
echo form_submit('mysubmit', 'Submit Post!');
echo form_close();

将浏览器定向到 localhost/testpost 显示带有单个提交按钮的页面。按下按钮会出现带有文本 "API index_post".

的屏幕

将浏览器发送到 localhost/API/3 会生成一个带有文本 "API find_get 3".

的屏幕

localhost/API 产生 "API index".

现在是有趣的事情(与您的问题无关,但很有趣)。

给定默认值

$route['default_controller'] = 'Apicontroller'; 

和路线

$route["API"]["get"] = "Apicontroller/index_get";

我预计将浏览器定向到主页 localhost 会产生 "API index"。但事实并非如此。它导致 404。由于这种行为,使用 default_controller

更明确可能是明智的
$route['default_controller'] = 'Apicontroller/index_get';

或者添加一个index()函数到调用$this->index_get()的Apicontroller。

我没有测试 PUT 或 DELETE,因为我的服务器未设置为处理它们。但是 GET 和 POST 似乎在起作用,在一个正义的世界里,它们会起作用。

您似乎在使用 PHil 的 REST_Controller 库和 CI 2.x,正确吗?

如果是这样,我建议您使用我喜欢称之为 "index gateway" 的东西,因为您不能使用 CI2:

进行按方法路由
class Apicontroller extends REST_Controller
{
  function index_gateway_get($id){
    $this->get_get($id);
  }

  function index_gateway_put($id){
    $this->put_put($id);
  }

  // This is not a "gateway" method because POST doesn't require an ID
  function index_post(){
    $this->post_post();
  }

  function get_get($id = null){
    if(!isset($id)){
      // Get all rows
    }else{
      // Get specific row
    }
  }

  function put_put($id = null){
    if(!isset($id)){
      // a PUT withtout an ID is a POST
      $this->post_post();
    }else{
      // PUT method
    }
  }

  function post_post(){
    // POST method
  }
}

完成这项工作的路由非常简单:

$route["API/(:num)"] = "Apicontroller/index_gateway/";

这就是您所需要的。 Phil 的 REST 库将根据使用的方法重定向到正确的 index_gateway_HTTPMETHOD。 然后每个 index_gateway_HTTPMETHOD 将重定向到正确的方法。

据我所知,这个技巧是让 CI2 使用适用于所有 HTTP 方法的单个 /API/ 入口点的唯一方法。