Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Codeigniter API REST don';t工作路线_Codeigniter_Api_Routes_Codeigniter Restserver - Fatal编程技术网

Codeigniter API REST don';t工作路线

Codeigniter API REST don';t工作路线,codeigniter,api,routes,codeigniter-restserver,Codeigniter,Api,Routes,Codeigniter Restserver,我正在使用codeigniter,用于制作api rest,以及提供官方网站的库 问题是:文件routes.php不能很好地重定向。当我将localhost/API/1放入浏览器时,显示404错误 这里是我的控制器“Apicontroller”: 和我的路线文件: $route['default_controller'] = 'Apicontroller'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE;

我正在使用codeigniter,用于制作api rest,以及提供官方网站的库

问题是:文件routes.php不能很好地重定向。当我将localhost/API/1放入浏览器时,显示404错误

这里是我的控制器“Apicontroller”:

和我的路线文件:

$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

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

使用上述路径,我创建了一些测试代码。这是那些文件

非常简化的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";
  }
}
我不相信,因为您的ApicController正在扩展一个不同的类,结果会改变。这可能是一个极端的假设

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

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

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

}
$route['default_controller'] = 'Apicontroller/index_get';
上面的代码加载的非常简单的视图(test.php)

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

$route["API"]["get"] = "Apicontroller/index_get";
我希望将浏览器指向主页localhost会产生“API索引”。但事实并非如此。结果是404。由于这种行为,明智的做法是使用默认的\u控制器更加明确

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

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

}
$route['default_controller'] = 'Apicontroller/index_get';
或者将
index()
函数添加到调用
$this->index\u get()
的Apicontroller


我没有测试PUT或DELETE,因为我的服务器没有设置来处理它们。但是,在一个公正的世界里,GET和POST似乎可以正常工作。

如果浏览器请求确实使用了/API,那么路由需要准确地“看到”这一点。此外,路由规则必须与要调用的方法显式关联。(希望显示的代码反映了您心目中的映射。)

使用上述路径,我创建了一些测试代码。这是那些文件

非常简化的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";
  }
}
我不相信,因为您的ApicController正在扩展一个不同的类,结果会改变。这可能是一个极端的假设

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

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

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

}
$route['default_controller'] = 'Apicontroller/index_get';
上面的代码加载的非常简单的视图(test.php)

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

$route["API"]["get"] = "Apicontroller/index_get";
我希望将浏览器指向主页localhost会产生“API索引”。但事实并非如此。结果是404。由于这种行为,明智的做法是使用默认的\u控制器更加明确

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

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

}
$route['default_controller'] = 'Apicontroller/index_get';
或者将
index()
函数添加到调用
$this->index\u get()
的Apicontroller


我没有测试PUT或DELETE,因为我的服务器没有设置来处理它们。但是,GET和POST似乎可以正常工作,在一个公正的世界中,它们会起作用。

似乎您正在将PHil的REST\u控制器库与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";
这就是你所需要的。Phil的REST库将重定向到正确的
index\u gateway\u HTTPMETHOD
,具体取决于所使用的方法。 然后,每个
索引\u网关\u HTTPMETHOD
将重定向到正确的方法


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

似乎您正在使用PHil的REST\u控制器库和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";
这就是你所需要的。Phil的REST库将重定向到正确的
index\u gateway\u HTTPMETHOD
,具体取决于所使用的方法。 然后,每个
索引\u网关\u HTTPMETHOD
将重定向到正确的方法


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

Error 404 localhost Apache/2.4.16(Unix)OpenSSL/1.0.1p PHP/5.6.12 mod_perl/2.0.8-dev perl/v5.16.3控制器的第一行看起来像这样吗{?它是否命名为(区分大小写)Apicontroller.php?它位于`应用程序/控制器/文件夹'中?请尝试将第一个路由更改为
$route[“API”]=“Apicontroller/index_get”;//basico
并在浏览器中输入'localhost/API'。找到任何地方了吗?当我尝试使操作得到“localhost/API/1”时,问题仍然存在如果我让“localhost/API”起作用。这是控制器的名称(在我的控制器文件夹中)Apicontroller.php,类是:“class Apicontroller extensed REST\u controller”。错误404 localhost Apache/2.4.16(Unix)OpenSSL/1.0.1p PHP/5.6.12 mod_perl/2.0.8-dev perl/v5.16.3控制器的第一行看起来像这样吗
类Apicontroller扩展了CI_控制器{
?它是否命名为(区分大小写)Apicontroller.PHP?它在`应用程序/控制器/文件夹中?尝试将第一条路由更改为
$route[“API”=“Apicontroller/index_get”;//basico
并在浏览器中输入'localhost/API'。如果我让'localhost/API'工作,那么当我尝试使操作得到'localhost/API/1'时,问题仍然存在。这是控制器的名称(在我的控制器文件夹中)Apicontroller.php,类是:”类Apicontroller扩展了REST\U控制器”。