Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter 2: not loading MY_Loader class

I have been trying to override the "database" method of the loader class (CI_Loader). I followed the instructions found on the CodeIgniter user guide: Creating Libraries (scroll to "Extending Native Libraries"). But MY_Loader class does not load automatically and is not used in $this->load calls in place of the CI core Loader class. I've only created MY_Loader class (application/libraries/MY_Loader.php as specified in the user guide). Is there something I'm missing? I've tried to put it in config/autoload.php for the libraries section of that file, and it indeed is autoloaded, but then I access to the library using $this->my_loader->database() and that's not the idea...

I paste below the content of 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;
    }
}

Thank you very much.

like image 416
Alejandro García Iglesias Avatar asked Dec 22 '22 16:12

Alejandro García Iglesias


2 Answers

The loader class is part of the core so it needs to go in "application/core/MY_Loader.php"

Any class you wish to extend should go in the corresponding directory in your applications folder. You should be able to take the class you wrote, drop it in there, and presto...it should work. No hacking required.

like image 60
Michael Copeland Avatar answered Jan 07 '23 00:01

Michael Copeland


Ok, I've managed to get it working, but I'm not sure if it's the best or most elegant way. First, I added "my_loader" to application/config/autoload.php on the libraries section. Then I checked what $this->load was inside a controller and it was CI_Loader instance, so in MY_Loader class' constructor I made a CI reference and replaced it's load property with a reference to MY_Loader: $CI->load = $this;.

The final MY_Loader class is this:

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();
    }
}

Please, if you come with a better/smarter solution, post an answer. Thanks.

like image 32
Alejandro García Iglesias Avatar answered Jan 07 '23 01:01

Alejandro García Iglesias