【问题标题】:Extend a controller Codeigniter扩展控制器 Codeigniter
【发布时间】:2015-01-22 11:39:19
【问题描述】:

我已经在 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");
        }


}

【问题讨论】:

  • 首先,在您扩展控制器之前一切正常吗?其次,您能否展示您的 MY_Controller 类的代码以及扩展它的控制器……并非所有这些都只是顶部。最后,文件名是否与类名完全匹配(区分大小写)?
  • 好的,罗斯,我已经添加了我的代码
  • 回答您的问题:每个不扩展 MY_Controller 的控制器都运行良好。 MY_Controller.php 文件名与“MY_Controller”的控制器名称完全匹配,包括大小写。
  • @GRY,只是为了排除一个问题,你有 $config['subclass_prefix'] = 'MY_';定义在 application/config/config.php (?)
  • @EduardUta 是的,先生。我已将 config 中的子类前缀定义为“MY_”

标签: php codeigniter


【解决方案1】:

将您的 MY_Controller 更改为:

<?php

class MY_Controller extends CI_Controller
{

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

}

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

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

【讨论】:

  • 我刚刚测试了代码,它不会用private static $instance; 或方法抛出错误……但它仍然不需要:)
猜你喜欢
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-10
  • 2017-09-13
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多