简体   繁体   中英

Loading a controller from another module in CodeIgniter HMVC

I have two modules setup in CodeIgniter HMVC . One is templates and another is test .

here is the folder structure ..

  1. templates
    • controllers
      • home.php
    • -----
      • ----.php
    • views
      • layout
        • admin.php
        • main.php
        • user.php
    • home.php
  2. test
    • controllers
      • test.php

I have added a route variable in routes.php which routes home.php as the default controller for templates. and auto loaded template library .

Now when i access http://mysite.com/templates/home/index or http://mysite.com/templates/ .. it works fine but when i run another module ( test ) it shows error . I have also tried echo Modules::run('templates/home/index'); but same problem . I have the flowing codes in 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();

    }
}

it says Unable to load the requested file: home.php

here is my template library

<?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 */

It seems that the module can be loaded without specifying the controller name only if the controller name matches the module name :

Controllers can be loaded as class variables of other controllers using $this->load->module('module/controller'); or simply $this->load->module('module'); if the controller name matches the module name

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

Try to load the module like that :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM