如何访问 CI HMVC 中的库?
how to access library in CI HMVC?
我当前的控制器和库如下所示...
>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' 是:
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' 文件是:
class Test_lib
{
function __construct(){
echo 'library loaded <br>';
}
function doSomething(){
echo 'it works!';
}
}
当我去 URL 'http://localhost/codeigniter-3.1.3/module1/test_cont'
它说:
---------------------------------------------------
| An Error Was Encountered |
---------------------------------------------------
| Unable to load the requested class: Test |
---------------------------------------------------
我希望我能让你理解我的问题,如何解决这个问题?...
(提前致谢)
库名称不区分大小写。对象实例将始终为小写。
function index(){
$this->load->library('Test_lib');
$this->test_lib->doSomething();
}
在 hmvc 中加载库、模型等时需要包含模块名称
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();
}
控制器如果没有 application/core/MY_Controller.php 使用 MX_Controller
文件名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 {
}
最后我发现脚本内部有一个错误,所以我虽然加载库有任何问题,但我的库已加载:
$this->load->library('Test_lib');
如果你在同一个模块中,那么你可以像这样加载库:
function index(){
$this->load->library('Test_lib');
$this->test_lib->doSomething();
}
但是如果你在不同的模块中并且你想从不同的模块加载库那么:
function index(){
$this->load->library('module_name/Test_lib');
$this->test_lib->doSomething();
}
我当前的控制器和库如下所示...
>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' 是:
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' 文件是:
class Test_lib
{
function __construct(){
echo 'library loaded <br>';
}
function doSomething(){
echo 'it works!';
}
}
当我去 URL 'http://localhost/codeigniter-3.1.3/module1/test_cont' 它说:
---------------------------------------------------
| An Error Was Encountered |
---------------------------------------------------
| Unable to load the requested class: Test |
---------------------------------------------------
我希望我能让你理解我的问题,如何解决这个问题?... (提前致谢)
库名称不区分大小写。对象实例将始终为小写。
function index(){
$this->load->library('Test_lib');
$this->test_lib->doSomething();
}
在 hmvc 中加载库、模型等时需要包含模块名称
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();
}
控制器如果没有 application/core/MY_Controller.php 使用 MX_Controller
文件名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 {
}
最后我发现脚本内部有一个错误,所以我虽然加载库有任何问题,但我的库已加载:
$this->load->library('Test_lib');
如果你在同一个模块中,那么你可以像这样加载库:
function index(){
$this->load->library('Test_lib');
$this->test_lib->doSomething();
}
但是如果你在不同的模块中并且你想从不同的模块加载库那么:
function index(){
$this->load->library('module_name/Test_lib');
$this->test_lib->doSomething();
}