简体   繁体   English

CodeIgniter - 默认控制器不自动加载

[英]CodeIgniter - Default controller not loading automatically

I have a default controller set:我有一个默认的控制器集:

$route['default_controller'] = "Home";

However, when I go to http://mydomain.com/ , it says 404 Page Not Found, but when going to http://mydomain.com/Home , the controller loads fine.但是,当我访问http://mydomain.com/ ,它显示 404 Page Not Found,但是当访问http://mydomain.com/Home ,控制器加载正常。 What could be the problem?可能是什么问题呢? I've been wracking my head for hours.我已经头疼了好几个小时了。 My htaccess is posted here if needed.如果需要,我的 htaccess 会在此处发布 Thanks!谢谢!

Turns out my problem was somewhat unrelated.原来我的问题有点无关。 I had to rename my default controller php file to lowercase and the controller class name to lowercase and everything started to work.我不得不将我的默认控制器 php 文件重命名为小写,将控制器类名重命名为小写,一切都开始工作了。 When CI looks for the default controller file, it searches in lowercase for the file;当 CI 查找默认控制器文件时,它会以小写形式搜索该文件; if I name my controller file "Home.php" instead of "home.php," CI misses it on Linux (since Linux file systems are case sensitive).如果我将我的控制器文件命名为“Home.php”而不是“home.php”,CI 在 Linux 上会错过它(因为 Linux 文件系统区分大小写)。

There is nothing wrong with your routing, the problem is with your htaccess file.您的路由没有任何问题,问题在于您的 htaccess 文件。 Try removing尝试删除

ErrorDocument 404 /index.php

You have probably some .htaccess problem.您可能有一些 .htaccess 问题。 Tray this way:托盘这样:

.htaccess .htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

config.php配置文件

$config['base_url'] = 'www.homepage.com';
$config['index_page'] = '';

routes.php路由文件

$route['default_controller'] = "yourcontrollername";
$route['404_override'] = '';

Without any additional information & code from your configs/controllers, I'd suggest checking config/config.php for these (both should be empty in normal cases):如果没有来自您的配置/控制器的任何其他信息和代码,我建议检查 config/config.php (在正常情况下两者都应该是空的):

$config['base_url'] = '';
$config['index_page'] = '';

IIRC, I have seen similar issue and it was related to these config variables. IIRC,我见过类似的问题,它与这些配置变量有关。 Hopefully this helps you a bit.希望这对你有所帮助。

I sure this .htaccess solved all ubuntu users...我确定这个 .htaccess 解决了所有 ubuntu 用户...

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

I have a default controller set:我有一个默认的控制器集:

$route['default_controller'] = "home";

it's working for me.它对我有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM