【问题标题】:CodeIgniter 3 REST Api cannot read routesCodeIgniter 3 REST Api 无法读取路由
【发布时间】:2021-11-30 20:15:12
【问题描述】:

我正在我的 REST API 中进行版本控制,并以这种方式构建它

example.domain.com
↳ v1
↳ v2

它将像https://www.example.domain.com/v1https://www.example.domain.com/v2 一样被浏览,其中每个版本文件夹都是 REST API 的根目录。 v1 文件夹中的所有内容都没有任何路由问题,但 v2 无法读取除 $route['default_controller'] = 'welcome'; 之外的路由,尽管 v2 实际上是 v1 的副本。我只更改了/application/config/config.php下的base_url,以遵循我上面提到的网址。

下面的代码是我在/application/config/routes.php 中的示例,并且 TestingController.php 位于/application/controllers/api/

$route['test'] = 'api/TestingController';  // new route
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;

这就是我在 TestingController.php 中的内容

<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
class TestingController extends REST_Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function index_get(){
        echo 'testing';
    }

}

当我浏览https://www.example.domain.com/v2/test 时,它会给出错误File not found.https://www.example.domain.com/v1/test 工作正常。我不知道在哪里看,因为两个版本文件夹中的代码相同。

我在每个版本的根文件夹中都没有任何 .htaccess 文件(可能是原因?),如果您想检查 REST_Controller,它是 here

另外,作为附加信息,我已从$config['index_page'] = ''; 下的/application/config/config.php 中删除了index.php,现在它是一个空字符串。


编辑:

我刚刚尝试添加.htaccess,但也不起作用。

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

它似乎正在使用 index.php 包括 https://www.example.domain.com/v2/index.php/test 。这很奇怪,因为 v1 没有它。

【问题讨论】:

    标签: routes codeigniter-3 versioning codeigniter-restserver


    【解决方案1】:

    检查您的网络服务器的 httpd.conf 文件并进行更改

    #LoadModule rewrite_module modules/mod_rewrite.so
    

    LoadModule rewrite_module modules/mod_rewrite.so
    

    在您的应用程序根文件夹中创建一个 .htaccess 文件

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 2017-08-21
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多