Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter+HMVC cross module call controller->method

I am pulling all my hair off... Have been searching every thread, would appreciate if someone can point me to a working example.

Accroding to the doc: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc I can call another module->controller using

modules::run('module/controller/method', $params);
modules::load('module/controller/method', $params);
or
$this->load->module('module/controller');
$this->controller->method();

Problem: the "method()" is never called. only constructor of the controller is called every time.

The objective is to build self-contained MVCs as module and use by other controllers. But no matter what I do, it only calls the constructor, method is not called. I started using HMVC a few weeks ago, did I miss something in the doc or it is not used this way?
Here is the setup:

modules
  |--ztest1
  |   |--controller/c1.php
  |--ztest2
      |--controller/c2.php

class C1 extends MX_Controller {
  function __construct() {
    parent::__construct();
  }
  function index () {
    Modules::run('ztest2/c2/testc2/');
    //Modules::load('ztest2/c2/testc2/');
    //$this->load->module('ztest2/c2/testc2/');
    //$this->c2->testc2();
  }
}

class C2 extends MX_Controller {
  function __construct() {
    parent::__construct();
    echo __FILE__." // ".__CLASS__."/".__FUNCTION__.PHP_EOL;
  }
  function testc2(){
    echo __FILE__." // ".__CLASS__."/".__FUNCTION__.PHP_EOL;
  }
}

output:
/app/modules/ztest2/controllers/c2.php // C2/__construct

additional note: no error or warning with the script. It just quietly calls the constructor.

like image 329
Reed Avatar asked Feb 18 '13 23:02

Reed


People also ask

What is HMVC modules in CodeIgniter?

Modular Extensions makes the CodeIgniter PHP framework modular. Modules are groups of independent components, typically model, controller and view, arranged in an application modules sub-directory that can be dropped into other CodeIgniter applications. HMVC stands for Hierarchical Model View Controller.

What are HMVC Module Controllers?

HMVC stands for Hierarchical Model View Controller. Module Controllers can be used as normal Controllers or HMVC Controllers and they can be used as widgets to help you build view partials.

What are modmodules in CodeIgniter?

Modules are groups of independent components, typically model, controller and view, arranged in an application modules sub-directory that can be dropped into other Codeigniter applications. This allows easy distribution of independent components (MVC) in a single directory across other CodeIgniter applications.

What is HMVC modular extension?

HMVC stands for Hierarchical Model View Controller. Modular Extensions make the Codeigniter PHP framework modular. Modules are groups of independent components, typically model, controller and view, arranged in an application modules sub-directory that can be dropped into other Codeigniter applications.


2 Answers

Thanks for MC's tip, I finally figured out the cause. HMVC doc indeed lacks some examples for beginner.

For anyone who may find this thread in the future, correct usage here:

to call module01/controller01/method00:

//method 1 CORRECT:
$ctlObj = modules::load('module01/controller01/');
$ctlObj->method00();
//or you could use chaining:
modules::load('module01/controller01/')->method00();

//method 1 WRONG:
modules::load('module01/controller01/method00');  //this will only load contructor

---
//method 2 CORRECT:
modules::run('module01/controller01/method00');   //no trailing slash!

//method 2 WRONG:
modules::run('module01/controller01/method00/');  

---
//method 3 CORRECT:
$this->load->module('module01/controller01');
$this->controller01->method00();

I don't understand why method 3 failed when I first try... maybe because I restarted HTTPD?

like image 147
Reed Avatar answered Oct 28 '22 07:10

Reed


This HMVC works well for me. I'm working on a project using this HMVC now. Just edit third_party/MX/Modules.php as shown in this link below and tell me the response.

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/pull-request/5/return-error-messages-instead-of-logging/diff

like image 29
Mansoorkhan Cherupuzha Avatar answered Oct 28 '22 08:10

Mansoorkhan Cherupuzha