gpt4 book ai didi

php - CodeIgniter HMVC 扩展了 MX_Controller,无法正确使用 get_instance

转载 作者:可可西里 更新时间:2023-11-01 12:56:59 24 4
gpt4 key购买 nike

我在/application/core 中有一个 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 MX_Controller {

public function __construct() {
parent::__construct();
}

public function show_something() {
echo "something shown";
}
}

我在从 CMS_Controller 扩展的模块 (/modules/my_module/controllers/controller.php) 中有另一个 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');
}
}

并且,在 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();
?>

我得到这个错误:

Fatal error: Call to undefined method CI::show_something() in /home/gofrendi/public_html/No-CMS/modules/my_module/views/view.php on line 3

如果我不使用 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";
}
}

有人知道这里出了什么问题吗?

最佳答案

在application/third_party/MX/Controller.php在构造函数的末尾(第 54 行之后)我加了

/* allow CI_Controller to reference MX_Controller */
CI::$APP->controller = $this;

如果您查看代码 $this 指的是当前类 MX_Controller 而 CI::$APP 指的是 CI_controller(查看 MX/Base.php 文件)

所以现在很简单...获取对 CI_Controller 的引用我们会做的(和往常一样)

    $this->CI =& get_instance();

为了获得对 MX_Controller 的引用,我们将做

    $this->CI =& get_instance()->controller;

关于php - CodeIgniter HMVC 扩展了 MX_Controller,无法正确使用 get_instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12586125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com