Codeigniter 3 - HMVC ::run() & ::load() problems

2019-07-14 03:43发布

问题:

I have issues when I try something as basic as, let's say, displaying a login form to a user : I have a Users controller in my module Users that has a function login_form() supposed to display the form. I also have a Controller Admin in my CI folder application/controllers/Admin.php which has a login() function that can call the user module.

The problem :

If I browse directly users/login_form, it works and calls the login_form() function.

But if Admin/login is browsed and calls Users/login_form as a module, I can't access use modules::run() - I have to use modules::load()

I tried with only a simple execution where class constructors and methods just write their name & loaded path (see full code below)

TEST A params

// URI called : admin/login
// Admin extends MX | Users extends MX 

// called from Admin::login()
$this->users = Modules::load('users')->login_form();

result

// class constructor - extends
Admin - MX_Controller
# Fx in Admin
Fx : login 

// class constructor - extends
Users - MX_Controller
# Fx in users
Fx : login_form

So while Modules::load() allows me to access the users/login_form method from Admin, I can't get Modules::run() working ...

TEST B (not working) params

// URI called : admin/login
// Admin extends MX | Users extends MX 

// called from Admin::login()
$this->users = Modules::run('users/login_form');

result

// same result as above for Admin

// class constructor - extends 
Users - MX_Controller 
# !! login_form() FROM USERS IS NOT EXECUTED !!

TEST A does execute the login_form() function, but although TEST B do load the constructor of the Users Class, it never reaches the login_form() function ...

I was wondering if it could be a routing problem, but as the constructor of the class Users is called in both run & load, I doubt this could be that ..

Full Code

Admin controller (application/controllers/Admin.php) :

class Admin extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();
        echo get_class().' - '.get_parent_class()." [".dirname(__FILE__)."]<br>\n";

    }

    function login()
    {
        echo  "Fx : ".__FUNCTION__."<br>\n";

        // works fine 
        // loads the Users class constructor
        // execute login_form()
        Modules::load('users')->login_form();

        // Does only load User constructor
        // Modules::run('users/login_form');
    }
}

Users Controller (application/modules/users/controllers/Users.php)

class Users extends MX_Controller {

    public function __construct(){
        parent::__construct();
        echo get_class().' - '.get_parent_class()." [".dirname(__FILE__)."]<br>\n";

    }

    public function login_form()
    {
        echo  "Fx : ".__FUNCTION__."<br>\n";
    }
}