我正在我的 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.php
from $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 在没有它的情况下工作。