CodeIgniter路由:我一直收到404错误

4
我有一个名为 application/controller/login.php 的文件,其中包含以下内容。
class Login extends Controller {

  function Login()
  {
    parent::Controller();  
  }

  function index()
  {
    $this->mysmarty->assign('title', 'Login');
    $this->mysmarty->assign('site_media', $this->config->item('site_media'));
    $this->mysmarty->display('smarty.tpl');
  }
}

我的路由定义如下:

$route['default_controller'] = "welcome";
$route['login'] = 'login';
$route['scaffolding_trigger'] = "";

问题在于,当我尝试访问http://localhost/myapp/login时,不断收到404错误。我做错了什么?我已经检查了CI路由文档,但没有发现任何问题。

首先,您需要告诉我们您使用的CI版本是哪个? 因为CI版本(3.x)要求控制器类名和文件名的第一个字母必须大写,所以如果您的CI版本是3.x,则可能与大小写有关。 - Jinesh Gandhi
3个回答

7

谢谢 v3,它解决了问题。但为什么default_controller不需要index.php呢? - Lorenzo
默认情况下,如果您访问 Apache Web 服务器上的文件夹,Apache 将查找 index.html、index.php、main.php 等文件。因此,http://localhost/myapp/ 相当于 http://localhost/myapp/index.php。 - v3.

2

需要注意的另一点是,如果你将"enable_query_strings"设置为true,并且没有使用.htaccess mod_rewrite规则:

在config/config.php文件中:

$config['enable_query_strings'] = TRUE;
$config['controller_trigger']   = 'c';
$config['function_trigger']     = 'm';
$config['directory_trigger']    = 'd';

为了正确路由您的请求,URL应该长这样:

http://localhost/myapp/index.php?c=login

0
如果你沒有使用 .htaccess 文件從應用程式中刪除 index.php,那就是另一回事了。但你可以這樣做:
設置一個非 welcome 檔案的默認控制器,如果你想要歡迎檔案成為你的默認,請使用以下方式連結。
http://localhost/myapp/index.php/login

请记住,此方法仅在尚未使用 .htaccess 文件删除 index.php 文件时才有效。

还需打开 application/config/config.php 文件,并使用此代码设置 baseurl

$config['base_url'] = 'http://localhost/myapp';

这应该总是起作用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接