简体   繁体   English

CodeIgniter 3 controller 方法在子目录中运行时不起作用

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

I am currently building a new sub-system for a client that will run on a sub-directory like https://example.com/new-subsystem .我目前正在为客户端构建一个新的子系统,它将在https://example.com/new-subsystem之类的子目录上运行。

Their existing website was native/hard-coded PHP so I have to create a new folder on the root and put the CodeIgniter 3 Framework there.他们现有的网站是本机/硬编码 PHP 所以我必须在根目录上创建一个新文件夹并将 CodeIgniter 3 框架放在那里。 I configured the base_url and voila.我配置了base_url,瞧。 It is working fine.它工作正常。

Now the problem is when I try to create a new method inside the controller, it returns error 404 that it seems like that the server is trying to handle the request literally and not let CodeIgniter handle it.现在的问题是,当我尝试在 controller 中创建一个新方法时,它返回错误 404,似乎服务器正试图从字面上处理请求,而不是让 CodeIgniter 处理它。

Example例子

When I try to access https://example.com/new-subsystem , it is working fine.当我尝试访问https://example.com/new-subsystem时,它工作正常。

But when I try to access https://example.com/new-subsystem/test it will show Error 404 and seems like the server is trying to find another folder instead.但是,当我尝试访问https://example.com/new-subsystem/test时,它会显示错误 404,并且似乎服务器正在尝试查找另一个文件夹。 Here is the actual route.php , config.php , controller which I believe that the problem is not on these three.这里是实际route.php , config.php , controller我相信问题不在于这三个。

routes.php路线.php

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

config.php config.php

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

Lockdown.php (Controller) 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!";
        }
    }

Main Question: How to tell the server that I want CodeIgniter to handle the rest of the request that is thrown under http://localhost/project-lockdown/tutorials/(controller)/(method) and not take it literally.主要问题:如何告诉服务器我希望 CodeIgniter 处理http://localhost .

For now, I am stuck here and still trying to find a solution to it across the internet and can find a thing.现在,我被困在这里,仍然试图通过互联网找到解决方案,并且可以找到东西。 I am no .htaccess guy so I think this has something to with the .htaccess magic or something (which I am trying to learn now).我不是.htaccess人,所以我认为这与.htaccess魔法或其他东西(我现在正在尝试学习)有关。

Any help would be appreciated.任何帮助,将不胜感激。 This problem will not occur if I just throw in CodeIgniter on the root folder.如果我只是在根文件夹中放入 CodeIgniter,则不会出现此问题。

Thank you in advance.先感谢您。

You need to append index.php in the URL to access your controller/method.您需要 URL 中的 append index.php 才能访问您的控制器/方法。 Try with -尝试 -

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

You can remove index.php by rewriting some rules in .htaccess file.您可以通过重写.htaccess文件中的一些规则来删除index.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CodeIgniter在子目录中不起作用 - CodeIgniter not working in sub-directory 是否有codeigniter控制器子目录限制? - Are there codeigniter controller sub-directory limits? 子目录中的控制器在codeigniter应用程序的托管服务器上不可访问 - controller in sub-directory not accessible on hosting server in codeigniter application 如何在 codeigniter v4 的子目录中创建 controller? - How to create a controller in a sub-directory in codeigniter v4? 在根目录和WordPress子目录中无法安装CodeIgniter - Installing CodeIgniter on root and WordPress in sub-directory Not Working 在root上安装CodeIgniter,在子目录中安装WordPress - Installing CodeIgniter on root and WordPress in sub-directory 如果在/根文件夹中还安装了CodeIgniter,则子目录中的CodeIgniter会给出404错误 - CodeIgniter in Sub-directory gives 404 error, when CodeIgniter is installed in / root folder also CodeIgniter 默认 controller 在子目录中不起作用 - CodeIgniter default controller in a sub directory not working Zend_Rest_Route不适用于子目录控制器(例如admin / questions) - Zend_Rest_Route not working for a sub-directory controller (e.g. admin/questions) 适用于旧应用程序的重写规则的Apache重写规则,用于在子目录中运行Symfony应用程序 - Apache rewrite rules for running a Symfony application in a sub-directory when rewrite rules already exist for legacy application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM