首页 文章

CodeIgniter 2:没有加载MY_Loader类

提问于
浏览
4

我一直试图覆盖加载器类(CI_Loader)的"database"方法 . 我按照CodeIgniter用户指南中的说明进行操作:Creating Libraries(滚动到"Extending Native Libraries") . 但是MY_Loader类不会自动加载,并且不会在 $this->load 调用中用于代替CI核心Loader类 . 我've only created MY_Loader class (application/libraries/MY_Loader.php as specified in the user guide). Is there something I'失踪了?我试图将它放在config / autoload.php中,用于该文件的库部分,它确实是自动加载的,但后来我使用 $this->my_loader->database() 访问该库,这不是主意......

我粘贴在application / libraries / MY_Loader.php的内容下面

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

class MY_Loader extends CI_Loader {

    function database($params = '', $return = FALSE, $active_record = NULL)
    {
        echo '---test---';
        exit;
    }
}

非常感谢你 .

2 回答

  • 0

    loader类是核心的一部分,因此需要进入“application / core / MY_Loader.php”

    您希望扩展的任何类都应该位于应用程序文件夹中的相应目录中 . 你应该能够把你写的课,放在那里,并预先......它应该工作 . 不需要黑客攻击 .

  • 10

    好吧,我've managed to get it working, but I'我不确定它是最好还是最优雅的方式 . 首先,我在库部分的application / config / autoload.php中添加了"my_loader" . 然后我检查了 $this->load 在控制器内部是什么,它是CI_Loader实例,所以在MY_Loader类' constructor I made a CI reference and replaced it'的load属性中引用了MY_Loader: $CI->load = $this; .

    最终的MY_Loader类是这样的:

    class MY_Loader extends CI_Loader {
    
        function __construct() {
            parent::__construct();
    
            $CI =& get_instance();
            $CI->load = $this;
        }
    
        function database($params = '', $return = FALSE, $active_record = NULL)
        {
            parent::database($params, $return, $active_record);
    
            // bootstrap doctrine
            require_once APPPATH . DIRECTORY_SEPARATOR . 'hooks' . DIRECTORY_SEPARATOR . 'doctrine' . EXT;
            bootstrap_doctrine();
        }
    }
    

    如果您有更好/更智能的解决方案,请发布答案 . 谢谢 .

相关问题