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

Phalcon Multi-Module Routing Issues

Im attempting to use the multi module configuration for the Phalcon PHP Framework.

Im having an issue relating to the routing as one of my modules is inaccessible via url. It seems every time I try to access the defined modules uri, it tries to invoke the default modules namespace.

Project\Module1\Controllers\Module2Controller handler class cannot be loaded

The idea solution

url.com/module1  => module1
url.com/moudle2  => module2
url.com          => module1

In working to resolve this problem, I have verified that all paths and namespaces are correct. Ive combed through it with xdebug.

Ive created my project with Phalcon Devtools with the multi module type defined as it provided me with a skeleton to follow. As I am using this method, it provided a default routing method that interprets each registered module and binds it to a url. This does not seem to be working for me. This is the Phalcon project before interpretation. https://github.com/phalcon/phalcon-devtools/tree/master/templates/project/modules

Routing

https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/routes.php

$router = $di->get("router");

foreach ($application->getModules() as $key => $module) {
    $namespace = str_replace('Module','Controllers', $module["className"]);
    $router->add('/'.$key.'/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 'index',
        'action' => 'index',
        'params' => 1
    ))->setName($key);
    $router->add('/'.$key.'/:controller/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 'index',
        'params' => 2
    ));
    $router->add('/'.$key.'/:controller/:action/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 2,
        'params' => 3
    ));
}

Module Registration

https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/modules.php

$application->registerModules(array(
    'module1' => array(
        'className' => 'Project\Module1\Module',
        'path' => __DIR__ . '/../apps/module1/Module.php'
    ),
    'module2' => array(
        'className' => 'Project\Module2\Module',
        'path' => __DIR__ . '/../apps/module2/Module.php'
    )
));

Service

https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/services.php

$di->set('router', function () {
$router = new Router();

$router->setDefaultModule('module1');
$router->setDefaultNamespace('Project\Module1\Controllers');

 return $router;
});

An example provided by Phalcon. This not exact to the Phalcon Tools implementation.

https://github.com/phalcon/mvc/tree/master/multiple-factory-default

Any help is much appreciated.

$router->add("/module1", [
        'module'     => 'module1',
        'namespace'  => 'module1\Controllers',
        'controller' => 'index',
        'action'     => 'index',
    ]);