0

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

example.domain.com
↳ v1
↳ v2

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

下面的代码是我所拥有的示例,/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 它就在这里

另外,作为附加信息,我已经删除了index.phpfrom $config['index_page'] = '';under /application/config/config.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 在没有它的情况下工作。

4

1 回答 1

0

检查您的 Web 服务器的 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]
于 2021-10-12T13:58:35.867 回答