1

私は公式のWebサイトを提供するライブラリで、APIの残りを作るためにcodeigniterを使用しています。

問題は、ファイル 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

CI 2.x で PHil の REST_Controller ライブラリを使用しているようですね。

その場合、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";

それだけです。Phil の REST ライブラリは、index_gateway_HTTPMETHOD使用されているメソッドに応じて正しいものにリダイレクトします。その後、それぞれindex_gateway_HTTPMETHODが正しいメソッドにリダイレクトされます。

私の知る限り、このトリックは、すべての HTTP メソッドで機能する単一の /API/ エントリ ポイントを CI2 に使用させる唯一の方法です。

于 2015-12-24T02:57:36.143 に答える