[英]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.