【问题标题】:CodeIgniter 3 controller methods not working when running in sub-directoryCodeIgniter 3 控制器方法在子目录中运行时不起作用
【发布时间】:2020-05-04 17:43:31
【问题描述】:

我目前正在为一个客户端构建一个新的子系统,它将在 https://example.com/new-subsystem 这样的子目录上运行。

他们现有的网站是原生/硬编码的 PHP,所以我必须在根目录上创建一个新文件夹并将 CodeIgniter 3 框架放在那里。我配置了base_url,瞧!它工作正常。

现在的问题是,当我尝试在控制器中创建一个新方法时,它返回错误 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) 下抛出的其余请求,而不是从字面上理解。

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

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

提前谢谢你。

【问题讨论】:

  • 我想你忘了从 URL 中删除 index.php。所以你需要在 URL 中附加 index.php 来访问你的控制器/方法。试试 - localhost/project-lockdown/tutorials/index.php/(controller)/…
  • 嗨@AlokMali,太棒了!它起作用了,我从来没有想过。现在我想我需要 .htaccess 来处理 index.php 的删除。谢谢!
  • 现在一切正常@AlokMali,非常感谢你。我现在删除了 .htaccess 上的 index.php,它现在可以工作了!再次感谢伙计。您可以将其发布为答案,我会接受吗?
  • 不客气的兄弟。
  • 我已将其添加为答案。

标签: php codeigniter-3


【解决方案1】:

您需要在 URL 中附加 index.php 才能访问您的控制器/方法。试试 -

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

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

【讨论】:

  • 此解决方案有效。有趣的是我怎么错过了这个。我编写了规则来删除 index.php 并将配置中的 site_url 设置为空。现在工作。谢谢兄弟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
相关资源
最近更新 更多