goF*_*ard 7 php codeigniter hmvc
我在/ application/core中有一个控制器
/application/core/CMS_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH."third_party/MX/Controller.php";
class CMS_Controller extends MX_Controller {
public function __construct() {
parent::__construct();
}
public function show_something() {
echo "something shown";
}
}
Run Code Online (Sandbox Code Playgroud)
我在模块中有另一个控制器(/modules/my_module/controllers/controller.php),它从CMS_Controller扩展而来
/modules/my_module/controllers/controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Controller extends CMS_Controller {
public function index() {
$this->load->view('view');
}
}
Run Code Online (Sandbox Code Playgroud)
并且,在view.php(/modules/my_module/views/view.php)中我这样做: /modules/my_module/views/view.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$ci =& get_instance();
echo $ci->show_something();
?>
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
致命错误:在第3行的/home/gofrendi/public_html/No-CMS/modules/my_module/views/view.php中调用未定义的方法CI :: show_something()
如果我不使用MX_Controller并使用CI_Controller,它将起作用: /application/core/CMS_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//require APPPATH."third_party/MX/Controller.php";
class CMS_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function show_something() {
echo "something shown";
}
}
Run Code Online (Sandbox Code Playgroud)
谁知道这里有什么问题?
在 application/third_party/MX/Controller.php 构造函数末尾(第 54 行之后)我添加了
/* allow CI_Controller to reference MX_Controller */
CI::$APP->controller = $this;
Run Code Online (Sandbox Code Playgroud)
如果你查看代码 $this 指的是当前类 MX_Controller 而 CI::$APP 指的是 CI_controller (查看 MX/Base.php 文件)
所以现在很简单......获取对 CI_Controller 的引用我们将做(按照正常情况)
$this->CI =& get_instance();
Run Code Online (Sandbox Code Playgroud)
为了获得对 MX_Controller 的引用,我们将做
$this->CI =& get_instance()->controller;
Run Code Online (Sandbox Code Playgroud)