CodeIgniter 2: do not load class MY_Loader

I am trying to override the database method of the loader class (CI_Loader). I followed the instructions in the CodeIgniter User Guide: Creating Libraries (go to the "Extending Your Own Libraries" section). But the MY_Loader class is not loaded automatically and is not used in calls to $this->load instead of the main CI loader class. I just created the class MY_Loader (application / libraries / MY_Loader.php, as indicated in the user manual). Is there something I'm missing? I tried putting it in config / autoload.php for the library section of this file, and it really is autoload, but then I access the library using $this->my_loader->database() , and this is not an idea ...

I embed the contents of the 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.

+4
source share
2 answers

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

Any class that you want to extend must be in the appropriate directory in the folder of your applications. You should be able to take the class you wrote, drop it there, and presto ... it should work. No hack required.

+10
source

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

The final MY_Loader class is the following:

 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 / reasonable solution, send a response. Thanks.

0
source

Source: https://habr.com/ru/post/1345383/


All Articles