2

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)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Amin
  • 681
  • 4
  • 9
  • 27

4 Answers4

2

The library names are not case sensitive. Object instances will always be lower case.

see creating libraries

function index(){
   $this->load->library('Test_lib');
   $this->test_lib->doSomething();
}
lumos0815
  • 3,908
  • 2
  • 25
  • 25
0

In hmvc you need to include the module name when loading library, model etc.

function index(){
   // You don't need to use upper case when loading library only class and filename

   $this->load->library('module-name/test_lib');
   $this->test_lib->doSomething();

   // Loading model hmvc

   $this->load->model('module-name/test_model');
   $this->test_model->doSomething();
}

Controller if no application/core/MY_Controller.php use MX_Controller

Filename Test_cont.php

class Test_cont extends MY_Controller
{

}

If you need to use MY_controller make sure you do this in application/core/MY_Controller.php

<?php

class MY_Controller extends MX_Controller {

}
0

finally I revealed there was an error inside script so I though there was any problem regarding loading the library, but my library loaded:

$this->load->library('Test_lib');
Amin
  • 681
  • 4
  • 9
  • 27
0

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();
}
Gaurav
  • 721
  • 5
  • 14