多个目录中的控制器出现 404 错误,
我正在使用 Codeigniter 2.0.1。例如,当我打电话时..
http://localhost/index.php/admin/login ./controllers/admin/login.php
它工作正常,但是当我使用 admin 目录中的文件夹调用下面的 url 时,它会抛出 404 错误。
http://localhost/index.php/admin/new_dir/dashboard ./controllers/admin/new_dir/dashboard.php
我的控制器已正确放置和命名。有谁知道为什么会发生这种情况,或者控制器只能是控制器目录深处的一个目录吗?
谢谢
I am using Codeigniter 2.0.1. When I call for example..
http://localhost/index.php/admin/login
./controllers/admin/login.php
It works fine but when I call the url below with a folder within the admin directory it throws the 404 error.
http://localhost/index.php/admin/new_dir/dashboard
./controllers/admin/new_dir/dashboard.php
My controllers are placed and named correctly. Does anyone know why this is happening or can controllers only be one directory deep from the controller directory?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,Codeigniter 中只能有一层子目录。仅将第一个 URI 段检查为可能的目录。
您是否希望有一个额外的子目录以保持整洁,或者这是为了您的 URI 的利益?如果是后者,您可以使用路由模拟额外子目录的行为。
Unfortunately you can only have one level of subdirectory in Codeigniter. Only the first URI segment is checked as a possible directory.
Do you wish to have an extra subdirectory for neatness sake or is this for the benefit of your URIs? If it is the latter you can emulate the behavior of extra subdirectories using routes.
是的,应用程序/控制器中可以有子文件夹。
routes.php:
$route['default_controller'] = "default_page";
将 *default_page.php* 添加到每个子文件夹和控制器文件夹本身,然后重定向到您需要用作该文件夹默认值的实际控制器,例如:
<块引用>
类 Default_Page 扩展 Public_Controller {
副作用:您的自定义 404 处理程序(如果有的话)会变得混乱:子文件夹不会被捕获,并且会抛出默认的 error_404。我仍在为此寻找合适的解决方案。
缺点:每个文件夹和子文件夹都有相似的文件,其中包含几乎相同的代码。假设您有一个包含许多文件夹的大型项目,这将变得难以维护(此时最好使用 HMVC)。我希望有人有更好的解决方案,但至少这是有效的。
Yes it is possible to have subfolders in application/controller.
routes.php:
$route['default_controller'] = "default_page";
Add a *default_page.php* to each sub-folder and the controller folder itself, than redirect to the actual controller you need to use as default for that folder, e.g.:
Side-effect: your custom 404 handlers - if you have any - will get messed up: sub-folders are not caught and will throw the default error_404. I'm still looking for a decent solution for this.
Disadvantage: You have similar files for each folder and sub-folder containing almost the same code. Suppose you have a large project with many folders this will become hard to maintain (at which point it would be better to use HMVC). I hope someone has a better solution but at the least this works.