【问题标题】:CodeIgniter routes: I keep getting a 404CodeIgniter 路线:我不断收到 404
【发布时间】:2010-10-17 11:54:32
【问题描述】:

我有一个 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。

标签: php codeigniter


【解决方案1】:

这应该没有什么问题 - 没有路由它是否可以工作? 另外,您是否正确设置了 .htaccess(即 http://localhost/myapp/index.php/login 是否工作)

【讨论】:

  • 感谢 v3 做到了。但是为什么default_controller不需要index.php呢?
  • 默认情况下,如果您转到 Web 服务器上的文件夹,Apache 会查找 index.html、index.php、main.php 等。所以localhost/myapp 等价于localhost/myapp/index.php
【解决方案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

【讨论】:

    【解决方案3】:

    如果您没有使用.htaccess 文件从您的应用程序中删除您的index.php,那么这本身就是另一个球类游戏。但是你可以这样做:

    设置一个默认的controller 不是welcome 文件,如果您希望欢迎文件成为您的默认文件,请以这种方式使用链接

    http://localhost/myapp/index.php/login
    

    请记住,这种方式仅适用于未使用 .htaccess 文件删除 index.php 文件的情况。

    同时打开apllication/config/config.php 并将此代码用于baseurl

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

    这应该总是有用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      相关资源
      最近更新 更多