扩展控制器Codeigniter

时间:2014-11-24 17:09:37

标签: php codeigniter

我在IIS上安装了Codeigniter。一切都很顺利。

我正在尝试扩展CI_Controller类。我这样做的原因是在构造函数中进行安全检查。一些控制器将扩展CI_Controller。一些需要安全的控制器将扩展MY_Controller。这对我来说似乎是一个很好的方法。

到目前为止,我已按照这些说明操作:

Creating Core Classes

但是当我调用一个扩展MY_Controller的页面时,我得到一个空白页面。 检查Firebug中的网络选项卡,我看到“500内部服务器错误”。 我检查了我的CI日志,我看到了这个:

DEBUG - 2014-11-24 10:26:47 --> Config Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Hooks Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Utf8 Class Initialized
DEBUG - 2014-11-24 10:26:47 --> UTF-8 Support Enabled
DEBUG - 2014-11-24 10:26:47 --> URI Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Router Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Output Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Security Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Input Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Global POST and COOKIE data sanitized
DEBUG - 2014-11-24 10:26:47 --> Language Class Initialized

现在,对于正常的参考点,当我加载任何不扩展MY_Controller的控制器时,我看到了:

DEBUG - 2014-11-24 11:00:14 --> Config Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Hooks Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Utf8 Class Initialized
DEBUG - 2014-11-24 11:00:14 --> UTF-8 Support Enabled
DEBUG - 2014-11-24 11:00:14 --> URI Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Router Class Initialized
DEBUG - 2014-11-24 11:00:14 --> No URI present. Default controller set.
DEBUG - 2014-11-24 11:00:14 --> Output Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Security Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Input Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Global POST and COOKIE data sanitized
DEBUG - 2014-11-24 11:00:14 --> Language Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Loader Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Helper loaded: url_helper
DEBUG - 2014-11-24 11:00:14 --> Session Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Helper loaded: string_helper
DEBUG - 2014-11-24 11:00:14 --> Session routines successfully run
DEBUG - 2014-11-24 11:00:14 --> Controller Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Database Driver Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Model Class Initialized
DEBUG - 2014-11-24 11:00:14 --> Model Class Initialized
DEBUG - 2014-11-24 11:00:14 --> File loaded: application/views/user/session_box.php
DEBUG - 2014-11-24 11:00:14 --> File loaded: application/views/public/public_menu.php
DEBUG - 2014-11-24 11:00:14 --> File loaded: application/views/public/welcome_message.php
DEBUG - 2014-11-24 11:00:14 --> File loaded: application/views/template.php
DEBUG - 2014-11-24 11:00:14 --> Final output sent to browser
DEBUG - 2014-11-24 11:00:14 --> Total execution time: 0.2295

谁能告诉我出了什么问题?

MY_Controller

class MY_Controller extends CI_Controller{

    private static $instance;

    /**
     * Constructor
     */
        function __construct()
        {
            parent::__construct();
        }

    public static function &get_instance()
    {
        return self::$instance;
    }
}

扩展它的控制器:

class Project extends MY_Controller{

    public function __construct()
        {
            parent::__construct();
            $this->load->database();        
            $this->load->model('user_model');
        }

    public function index()
    {
            var_dump("hi");
        }


}

1 个答案:

答案 0 :(得分:1)

将MY_Controller更改为:

<?php

class MY_Controller extends CI_Controller
{

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

}

这是因为$this已经包含了实例,因为它正在扩展CI_Controller

我不能确定这是问题,但我希望它有所帮助!

相关问题