We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Single Module to Multiple Module App with some problems

Hello everybody, I would like to refactor my project from a single module application to a multiple module app.

My structure looks like:

/apps
  /moduleA
      /controllers
      /views
      /Module.php
  /moduleB
      /controllers
      /views
      /Module.php
  ...

Every Module has a Module.php which looks like:

public function registerAutoloaders(DiInterface $di = null)
{
    $loader = new Loader();
    $loader->registerNamespaces(
        [
            "Mod\\GeneralController" => "../app/general/controllers/",
        ]
    );
    $loader->register();
}

public function registerServices(DiInterface $di)
{
    // Registering a dispatcher
    $di->set(
        "dispatcher",
        function () {
            $dispatcher = new Dispatcher();
            $dispatcher->setDefaultNamespace("Mod\\GeneralController");
            return $dispatcher;
        }
    );
    $di->set(
        "view",
        function () {
            $view = new View();
            $view->setViewsDir("../app/general/views/");
            return $view;
        }
    );
}

Somewhere I'm adding my routes like:

$di->setShared('router', function() {
  $router = new \Phalcon\Mvc\Router\Annotations(false);

  $router->setDefaultModule('general');
  ...
  $router->addResource('Mod\OtherController\Something', '/something');
  ...

  $router->add('/:controller', [
      'controller' => 1,
      'action'     => 'index',
  ]);

  $router->add('/:controller/:action', [
      'controller' => 1,
      'action'     => 2,
  ]);

  $router->add('/:controller/:action/:int', [
      'controller' => 1,
      'action'     => 2,
      'id'         => 3,
  ]);

  return $router;
});

And my Index.php I'm registering my modules, it looks like these:

$application->registerModules(
    [
        'other_managment' => [
            'className' => 'Easy\OtherController\Module',
            'path' => '../app/other_managment/Module.php'
        ],
        'general' => [
            'className' => 'Easy\GeneralController\Module',
            'path' => '../app/general/Module.php'
        ],
    ]
);

echo $application->handle()->getContent();

My problem is, that I'm not sure, that this strucure is the right way? And I got some strange problems that im not able to figure out. My biggest problem for now is, that I get these error:

Mod\GeneralController\IndexController handler class cannot be loaded

The problem is, that IndexController is a part of some other Module not part of GeneralController, but it extends a Baseclass which is part of GeneralController.

Do someone have some clues how I can get this working or where I have to set my focus?

Theres another Topic about it, that seems to be solved: https://forum.phalcon.io/discussion/4624/multiple-module

There were several diffrent problems, I found a solution by this: https://github.com/phalcon/mvc and other examples