我使用WireDesignz HMVC,并有一个使用CodeIgniter 3.0.6 的KERNEL_Controller(实际上是带有另一个前缀的MY_Controllers)
class KERNEL_Controller extends MX_Controller {
public $myvar;
public function __construct() {
parent::__construct();
//Load core Model
$this->load->model('Core_model');
$this->myvar = 'test';
}
}
现在我有一个助手,它可以完成以下
function Editor($id) {
$ci =& get_instance();
echo $ci->myvar;
}
当我调用视图中的编辑器时,我会得到
Severity: Notice
Message: Undefined property: CI::$myvar
Filename: helpers/editor_helper.php
在编辑器帮助程序中调用方法时相同
在KERNEL_Controller中,我有
public function DoTest() {
echo '1';
}
在Editor_helper 中
function Editor($id) {
$ci =& get_instance();
echo $ci->DoTest();
}
我知道,当调用视图中的编辑器时
Type: Error
Message: Call to undefined method CI::DoTest()
但是当我调用编辑器助手中的模型时
function Editor($id) {
$ci =& get_instance();
$result_content = $ci->Core_model->GetLanguages(1);
print_r($result_content);
}
我确实得到了(在本例中)一个返回了语言的数组。
哦,是的!!我就是那个男人,我搞定了!考虑创建我"自己的"get_instance()系统,我做了以下操作:
class MY_Controller extends MX_Controller
{
public static $instance;
function __construct()
{
self::$instance || self::$instance =& $this;
...
}
}
然后在图书馆里,或者在助手里,或者任何你需要使用它的地方:
$CI =& MY_Controller::$instance;
注意,如果您自动加载库,如果它在库的__construct()中,则MY_Controller::$instance将不起作用,因为MY_Coontroller尚未定义
https://stackoverflow.com/questions/16433210
根据https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc你必须使用类似的东西
<?php
/** module and controller names are different, you must include the method name also, including 'index' **/
modules::run('module/controller/method', $params, $...);
/** module and controller names are the same but the method is not 'index' **/
modules::run('module/method', $params, $...);
/** module and controller names are the same and the method is 'index' **/
modules::run('module', $params, $...);
/** Parameters are optional, You may pass any number of parameters. **/
我不知道你的模块是怎么命名的,但你可以试试
modules::run('module/KERNEL/DoTest');
以便调用CCD_ 2控制器的方法CCD_。