CI 新的默认控制器不工作

CI new default controller not working

CI 3.0 有问题 如果我将默认控制器保留在 routes.php 文件 "welcome" 中,一切正常。 但是如果我改变它,即 "main" CI 开始抛出 404 错误 第一步的主控制器与欢迎相同。我只是复制了文件。重命名,更改 class 名称(当然),并在 index() 加载视图中。 有什么建议吗?

我也忘了说 在 wamp localhost 上一切正常.. 但在服务器上不是.. :/

还有一件事…… 即如果我尝试转到 mydomain.com/welcome - 工作, 如果我尝试转到 mydomain.com/main - 不。 即使我将路由默认控制器改回欢迎

我的 main.php 文件:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends CI_Controller {
    function index(){
        $this->load->view('welcome_message');
    }
}

我的routes.php文件:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'main';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

尝试将 function index() 更改为 Public。 如果这不起作用,则尝试在 URL 中添加:domain.com/index.php/main 看看会发生什么。有时您需要在另一台服务器中使用 .htaccess 来删除 index.php.

如评论中所述:您的控制器文件名必须以大写字母开头。在你的情况下,Main.php。 参见 http://codeigniter.com/userguide3/changelog.html

«更改了文件命名约定(class 文件名现在必须是 Ucfirst,其他一切都小写)。 »

我遇到了这个问题。 我修复它只是替换路径。 这里 "site" 文件夹是默认文件夹,如果你想使用 CI,你必须将所有代码存储到 "site" 文件夹中。如果您没有使用站点文件夹进行安装,它将无法使用多控制器设置。 因此你得到默认控制器作为一个可行的,其余的出现 404.

更改根目录中的 .htacess 文件。

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /

        # Removes index.php from ExpressionEngine URLs
        RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
        RewriteCond %{REQUEST_URI} !/system/.* [NC]
        RewriteRule (.*?)index\.php/*(.*) / [R=301,NE,L]

        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
       # RewriteRule ^(.*)$ /site/index.php/ [L]
        RewriteRule ^(.*)$ /codeig/index.php/ [L]
</IfModule>

上面你可以看到
RewriteRule ^(.*)$ /codeig/index.php/ [L] "codeig" 根文件夹的更改行。

在 Codeigniter 3 中,他们添加了额外的代码来强制文件名以大写字母开头。这是此问题的修复程序。

system/core/Router.php换行(约303

if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))

if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))

我还必须删除 CodeIgniter.phpLoader.php 中的内容。

您只需要在 application/config/routes.php 中设置您的默认控制器,例如

$route['default_controller'] = $this->set_directory('front/home/').'home/index';

对于 Ci 3.x