当子目录中的 运行 时,CodeIgniter 3 控制器方法不起作用

CodeIgniter 3 controller methods not working when running in sub-directory

我目前正在为客户端构建一个新的子系统,该系统将 运行 在像 https://example.com/new-subsystem.

这样的子目录上

他们现有的网站是 native/hard-coded PHP 所以我必须在根目录下创建一个新文件夹并将 CodeIgniter 3 Framework 放在那里。我配置了 base_url 瞧!它工作正常。

现在的问题是,当我尝试在控制器中创建一个新方法时,它 returns 错误 404 似乎是服务器试图按字面意思处理请求,而不是让 CodeIgniter 处理它。

例子

当我尝试访问 https://example.com/new-subsystem 时,它工作正常。

但是当我尝试访问 https://example.com/new-subsystem/test 时,它会显示错误 404,服务器似乎正在尝试寻找另一个文件夹。这是实际的 route.phpconfig.phpcontroller 我相信问题不在这三个上。

routes.php

$route['default_controller'] = 'lockdown';
$route['test'] = 'lockdown/test';

config.php

$config['base_url'] = 'http://localhost/project-lockdown/tutorials/';

Lockdown.php(控制器)

    class Lockdown extends CI_Controller {
        public function index()
        {
            // This is working by default. It shows on http://localhost/project-lockdown/tutorials/
            echo "Hello World!"
        }
        public function test(){
        // This should show on http://localhost/project-lockdown/tutorials/test
            echo "Hello, this is a test method and it is not working. Error 404 is shown!";
        }
    }

主要问题: 如何告诉服务器我希望 CodeIgniter 处理在 http://localhost/project-lockdown/tutorials/(controller)/(method) 下抛出的其余请求,而不是字面意思。

目前,我被困在这里,仍在尝试通过 Internet 找到解决方案并且可以找到一个东西。我不是 .htaccess 人,所以我认为这与 .htaccess 魔术或其他东西有关(我现在正在努力学习)。

如有任何帮助,我们将不胜感激。如果我只是将 CodeIgniter 放在根文件夹中,就不会出现此问题。

提前致谢。

您需要在 URL 中附加 index.php 才能访问您的 controller/method。试试 -

localhost/project-lockdown/tutorials/index.php/(controller)/

您可以通过重写 .htaccess 文件中的一些规则来删除 index.php