从CodeIgniter HMVC中的另一个模块加载控制器


Loading a controller from another module in CodeIgniter HMVC

我在CodeIgniter HMVC中设置了两个模块。一个是模板,另一个是测试。

这是文件夹结构。。

  1. 模板
    • 控制器
      • 主页.php
    • -----
      • ----.php
    • 视图
      • 布局
        • admin.php
        • main.php
        • user.php
    • 主页.php
  2. 试验
    • 控制器
      • test.php

我在routes.php中添加了一个路由变量,它将home.php作为模板的默认控制器进行路由。以及自动加载的模板库。

现在当我访问http://mysite.com/templates/home/index或http://mysite.com/templates/。。它运行良好,但当我运行另一个模块(测试)时,它显示错误。我也试过echo Modules::run('templates/home/index');,但有同样的问题。我有test.php 中的流动代码

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends MX_Controller {

    public function index()
    {
       $this->load->module('templates');
       $this->templates->index();
    }
}

上面写着Unable to load the requested file: home.php

这是我的模板库

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Template {
    private $template_data = array();
    private $headers = array();
    private $CI;
    public function __construct() {
        $this->CI =& get_instance();
        $this->CI->config->load('template');
    }
    function set($name, $value) {
        $this->template_data[$name] = $value;
    }
    function add_header($header) {
        array_push($this->headers, $header);
    }
    function load($template = '', $view = '', $view_data = array(), $return = FALSE) {
        $this->CI = & get_instance();
        $this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
        $this->set('headers', implode('', $this->headers));
        return $this->CI->load->view($template, $this->template_data, $return);
    }
}
/* End of file Template.php */
/* Location: ./system/application/libraries/Template.php */

似乎只有当控制器名称与模块名称匹配时,才可以在不指定控制器名称的情况下加载模块:

控制器可以作为其他控制器的类变量加载使用$this->load->module(‘module/controller’);或者简单地$this->load->module("module");如果控制器名称与模块名称

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview

尝试这样加载模块:

$this->load->module('templates/home');