Search code examples
phpcodeignitercodeigniter-3hmvccodeigniter-hmvc

how to access library in CI HMVC?


my current controller & library are listed follow...

>application/
 - config/
 - controllers/
 - ...
 - models/
 - modules/
   - module1/
     - controllers/
       - Test_cont.php
     - models/
     - views/
     - libraries
       - Test_lib.php
 - third_party/
 - views/
 - ...(other files & folders)

'modules/module1/controllers/Test_cont.php' is:

class Test_cont extends MY_Controller
{
  function __construct(){
    parent::__construct();
  }

  function index(){
    $this->load->library('Test_lib');
    $this->Test_lib->doSomething();
  }
}

'modules/module1/libraries/Test_lib.php' file is:

class Test_lib
{
  function __construct(){
    echo 'library loaded <br>';
  }

  function doSomething(){
    echo 'it works!';
  }
}

when I go to the URL 'http://localhost/codeigniter-3.1.3/module1/test_cont' it says:

---------------------------------------------------
| An Error Was Encountered                        |
---------------------------------------------------
| Unable to load the requested class: Test        |
---------------------------------------------------

I hope I could make you understand my problem, how to solve this?... (Thanks in advance)


Solution

  • If you are in same module then you can load library like this:

    function index(){
       $this->load->library('Test_lib');
       $this->test_lib->doSomething();
    }
    

    but if you are in different module and you want to load library from different module then:

    function index(){
       $this->load->library('module_name/Test_lib');
       $this->test_lib->doSomething();
    }