Loading a controller from another module in CodeIgniter HMVC

shahriar31 picture shahriar31 · Jul 4, 2013 · Viewed 19.8k times · Source

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

Answer

Chuck picture Chuck · Jul 4, 2013

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');