Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

php - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...