Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to load from another module to another in HMVC?


I've been working on a CRM like application. I'm using HMVC pattern to separate the modules. I have a need to call models across the system from different modules. When I call a model from a controller which is belongs to the same module it's loading but when I call from another module it's giving a Fatal Error is there any limitation on this or am I doing it wrong.

Namespaces are correct, double checked on them.

edited Oct '17

Just register all models(and other common classes) outside of modules. Like before registering modules etc.


Can't we keep models separatly under the respective module directory?

edited Oct '17

You can still keep them seperately under respective module directory. Just register them outside:

$modules = $application->getModules();
 foreach ($modules as $key => $value) {
        $module = ucfirst($key);
        $namespaces += ["Suzuki\\Module\\{$module}\\Model" => "../Module/{$module}/Model"];
        if ($module != 'Error') {
            $routeClasses += ["Suzuki\\Module\\{$module}\\Routes" => "../Module/{$module}/Routes.php"];
            $namespaces += ["Suzuki\\Module\\{$module}\\Repository" => "../Module/{$module}/Repository"];


Example of code from my app.


Thank you for the reply.

I'm registering all the modules on the loader. So I thought namespaces will be registered at that point. If there any way to dynamic module registration?

edited Oct '17

No, namespaces of each module are registered only when this module is used by dispatcher. When router will return route using ABC module then dispatcher will actually use this module and call $module->registerNamespaces etc. Methods like Module::registerNamespaces are not called when doing $application->registerModules()

Yea there is - use DirectoryIterator class.

edited Oct '17

I do implemented this method registerAutoloaders and registering under all of my Module.php and registering the namespaces there. Is there any way to trigger these.

edited Oct '17

They are triggered when the module is used by dispatcher as i wrote above, like if router resolves your url to route using this module then dispatcher will call those methods NOT WHEN using $application->regsiterModules. Just think about method registerNamespaces in your module as namespaces for only this module and that's it. If you need any other namespace to be used across modules just register them seprately in some bootstrap file.

Why you no use sub controllers:

I use this instead Modules:

  • Controllers independent
  • ControllerBase shared
  • Forms independent
  • FormsBase shared
  • Views independent
  • Main layout shared
  • Models shared