codeigniter+HMVC cross module call controller->method
Asked Answered
G

7

11

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.

Gathers answered 18/2, 2013 at 23:33 Comment(0)
L
2

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

Laevorotation answered 19/2, 2013 at 1:15 Comment(4)
Thanks for the link! I got this message: Module controller failed to run: ztest2/c2/testc2/Gathers
It's solved. I have put my finding in answer below. I accepted your answer because it helped me debug this thing. Did not know HMVC throw error to log only. thanks!Gathers
@Reed, Now i'm working on a project with this HMVC so I'm learning through thisLaevorotation
@Gathers The accepted answer should be the actual answer which solves the problem.Addition
G
24

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?

Gathers answered 19/2, 2013 at 15:12 Comment(5)
Thank you Reed. This will help beginners. UPVOTE :)Laevorotation
@MostafaShahverdy what is not working for you? all 3 methods?Gathers
@Reed, None of them :-(Nearsighted
This is an awesome explanation. Too bad I can only give one thumbs up. :)Lamar
@Gathers If I use same controller name in the other module, and run that module's method in a controller that having the same name, it throws an error saying "call to undefined method".Addition
L
2

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

Laevorotation answered 19/2, 2013 at 1:15 Comment(4)
Thanks for the link! I got this message: Module controller failed to run: ztest2/c2/testc2/Gathers
It's solved. I have put my finding in answer below. I accepted your answer because it helped me debug this thing. Did not know HMVC throw error to log only. thanks!Gathers
@Reed, Now i'm working on a project with this HMVC so I'm learning through thisLaevorotation
@Gathers The accepted answer should be the actual answer which solves the problem.Addition
C
2

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
Cholera answered 27/4, 2016 at 17:55 Comment(0)
J
1

After some attempts to achieve call a controller that is not located within any module.

Modules::run('../Controller/method');
Jerrybuilt answered 17/9, 2016 at 3:41 Comment(0)
P
0

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

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

Parton answered 28/3, 2015 at 13:38 Comment(0)
A
0

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

Aeroballistics answered 9/9, 2015 at 6:19 Comment(0)
D
0
  /*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>";
Donofrio answered 19/12, 2018 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.