php codeigniter+HMVC 跨模块调用控制器->方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14947261/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter+HMVC cross module call controller->method
提问by Reed
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-hmvcI can call another module->controller using
根据文档:https: //bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc我可以调用另一个模块->控制器使用
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.
问题:永远不会调用“method()”。每次只调用控制器的构造函数。
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:
目标是将独立的 MVC 构建为模块并供其他控制器使用。但是无论我做什么,它都只调用构造函数,不调用方法。几周前我开始使用 HMVC,我是否遗漏了文档中的某些内容,或者它没有以这种方式使用?
这是设置:
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.
附加说明:脚本没有错误或警告。它只是悄悄地调用构造函数。
采纳答案by Mansoorkhan Cherupuzha
This HMVC works well for me. I'm working on a project using this HMVC now.
Just edit third_party/MX/Modules.phpas shown in this link below and tell me the response.
这个 HMVC 很适合我。我现在正在使用这个 HMVC 进行一个项目。只需third_party/MX/Modules.php按照下面的链接所示进行编辑,然后告诉我回复。
回答by Reed
Thanks for MC's tip, I finally figured out the cause. HMVC doc indeed lacks some examples for beginner.
感谢MC的提示,我终于找到了原因。HMVC 文档确实缺少一些适合初学者的示例。
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?
我不明白为什么我第一次尝试时方法 3 失败了……也许是因为我重新启动了 HTTPD?
回答by WindDude
I ran into the same issue. Make sure you check capitalization of your directories and and controllers. It's not case sensitive for differing between the module and controller name.
我遇到了同样的问题。确保检查目录和控制器的大小写。模块和控制器名称之间的差异不区分大小写。
//In my case the below did not work
$this->load->module('dashboard/Dashboard');
$this->Dashboard->method();
//but 
$this->load->module('dashboard');
$this->Dashboard->method();
//worked
回答by WindDude
After some attempts to achieve call a controller that is not located within any module.
经过一些尝试实现调用不位于任何模块内的控制器。
Modules::run('../Controller/method');
回答by anand
  /*echo Modules::run("controller name of a module which you want to call/and its.. function name");*/
echo Modules::run("Second/callit");
or
或者
$this->load->module('Second');
$this->second->callit();
But.. the controller name shold be different .. from one module to another module..
但是..控制器名称应该不同..从一个模块到另一个模块..
                       **(parameter passing)**
        echo "<hr>";
       //echo Modules::run("controller name of a module which you want to call/and its.. function name");
         $data="peter";
      echo Modules::run("Second/callit",$data);
      echo "<hr>";
      $this->load->module('Second');
     $this->second->callit($data);
      echo "<hr>";
回答by Chas
I am new at CI as well, and I thought I was having the same issue. Script seemed not to be running. (no html output).
我也是 CI 的新手,我以为我遇到了同样的问题。脚本似乎没有运行。(没有 html 输出)。
//This did NOT work (did not produce output)
modules::run('module_name/method_name',$data); 
// but this DID work???  didn't know why
modules::run('module_name/method_name',$data); 
exit();
// turns out you need the echo for output
echo modules::run('templates/login_template',$data); 
This may be obvious to many of you- but I wasted two hours searching for an answer.
这对你们中的许多人来说可能很明显 - 但我浪费了两个小时来寻找答案。
回答by TaraGurung
so According to the documentation they says copy the controller in default controller folder and move to modules controller.
所以根据他们所说的文档,将控制器复制到默认控制器文件夹中并移动到模块控制器。
So now how do I run the controller that has been moved to modules when I run its running from the default controller file if removed does not work so how to make it run the controller inside module as a default controller to run.
所以现在我如何运行已经移动到模块的控制器,如果我从默认控制器文件运行它,如果删除不起作用,那么如何让它运行模块内的控制器作为默认控制器运行。
So Do I need to mention the modules name too in the route
所以我需要在路由中提到模块名称吗?

