1

我正在使用 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/$1"; //update
$route["Apicontroller/(:num)"]["delete"] = "Apicontroller/index/$1"; //delete
4

2 回答 2

0

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

/*sub-rutas*/
/*---------*/
$route["API"]["get"] = "Apicontroller/index_get"; //basico
$route["API/(:num)"]["get"] = "Apicontroller/find_get/$1"; //select
$route["API"]["post"] = "Apicontroller/index_post"; //insert
$route["API/(:num)"]["put"] = "Apicontroller/index_put/$1"; //update
$route["API/(:num)"]["delete"] = "Apicontroller/index_delete/$1"; //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 扩展了一个不同的类,结果会改变。这可能是一个极端的假设。

为了测试 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 索引”。

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

给定默认值

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

和路线

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

我希望将浏览器定向到主页localhost会产生“API 索引”。但事实并非如此。它会导致 404。由于这种行为,使用 default_controller 更明确可能是明智之举

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

index()或者向 Apicontroller添加一个调用$this->index_get().

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

于 2015-12-16T16:46:11.233 回答
0

好像您正在使用 PHil 的 REST_Controller 库和 CI 2.x,对吗?

如果是这样,我建议您使用我喜欢称之为“索引网关”的东西,因为您不能使用 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/$1";

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

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

于 2015-12-24T02:57:36.143 回答