即使调用了控制器,也不会触发CodeIgniter索引函数


CodeIgniter index function not triggered even though the controller has been called

下面的代码示例没有回显"Here I am!"它不会从_construct函数回显。如果我将echo语句移到类的上方,它就会回显,这样我就知道CI到达了这个控制器。

基本信息:

我继承了一个用CodeIgniter 2.1.2完成的项目。我应该将应用程序复制到具有不同子域的另一个目录,并将数据库配置指向另一个数据库。我在配置中更新了数据库。我已经更新了基础浴。我将环境设置为development,因此可以获得错误消息。没有错误

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
if (!ini_get('date.timezone')) {
    date_default_timezone_set('my-time-zone');
}
class Login extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('mod_login');
        $this->load->helper('date');
    }
    function index() {
         echo "Here I am!"; //Nothing echos 
    }
   function logout{
   //logout function here.
   }
}
有CodeIgniter的粉丝知道为什么吗?

你的控制器应该是这样的。

<?php    
    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    class Login extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
            $this->load->model('mod_login');
            $this->load->helper('date');
        }
        public function index()
        {
            echo "Here I am!";
        }

     }

,控制器名称为loging.php

config/routes.php

$route['default_controller'] = "login";/set default conntoller

附加说明:(删除URL中的index.php)

in config/config.php

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

.htaccess(放在应用程序文件夹外)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule>

控制器的类名必须为大写。从文档:

注意:类名必须以大写字母开头。

也就是说,这是有效的:

<?php
class Blog extends CI_Controller {
}
?>

This is not valid:

<?php
class blog extends CI_Controller {
}
?> 

: https://ellislab.com/codeigniter/user-guide/general/controllers.html你好