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

multi modules router problem

envirment: win7x64, WAMP-2.5 phalcon-2.0.0

I copied multiple from

https://github.com/phalcon/mvc

and it's easy to access the samples

but if I wan't to add some more controllers and actions in "backend" module, the router would guite the program to the frontend module

<?php

error_reporting(E_ALL);

class Application extends \Phalcon\Mvc\Application {

/**
 * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
 */
protected function _registerServices() {

    $di = new \Phalcon\DI\FactoryDefault();

    $loader = new \Phalcon\Loader();

    /**
     * We're a registering a set of directories taken from the configuration file
     */
    $loader->registerDirs(
        array(
            __DIR__ . '/../apps/library/',
        )
    )->register();

    //Registering a router
    $di->set('router', function () {

        $router = new \Phalcon\Mvc\Router();

        $router->setDefaultModule("Entrance");

        $router->add('/:controller/:action', array(
            'module' => 'entrance',
            'controller' => 1,
            'action' => 2,
        ));

        $router->add("/functions1/:controller/:action", array(
            'module' => 'function1',
            'controller' => 1,
            'action' => 2,
        ));

        return $router;

    });

    $di->set('url', function () {
        $url = new \Phalcon\Mvc\Url();
        $url->setBaseUri('/myproject');
        return $url;
    });

    $this->setDI($di);
}

public function main() {

    $this->_registerServices();

    //Register the installed modules
    $this->registerModules(array(
        'Entrance' => array(
            'className' => 'Multiple\Entrance\Module',
            'path' => '../apps/entrance/Module.php',
        ),
        'Impellerhelper' => array(
            'className' => 'Multiple\Function1\Module',
            'path' => '../apps/Function1/Module.php',
        ),
    ));

    echo $this->handle()->getContent();
}

}

$application = new Application(); $application->main();

for example it's easy to access

https://localhost/myproject/entrance

https://localhost/myproject/function1

but if I add SomeController in function1 module, it will fail

https://localhost/myproject/function1/some

the error message is

Phalcon\Mvc\Dispatcher\Exception: Multiple\Entrance\Controllers\SomeController handler class cannot be loaded

Can anyone help me?

edited Apr '15

It looks like is matching the first router from entrance module and doesn't search further.

If you want to access only by controller add a route for that too, otherways it will search for the default module controller

$router->add("/function1/:controller", array(
      'module' => 'function1',
      'controller' => 1,
      'action' => 'index',
  ));

And as a tip, since you already setDefaultModule('Entrance') you no longer need to define that first (:controller/:action) route since it will take the default routing settings(except if you need something custom).

Thank you for your comment.

I tried your suggestion, but it doesn't work.

Even when I access

https://localhost/myproject/function1

it can't display, and same error message.

for that you have to define another route

$router->add("/function1", array(
    'module' => 'function1',
    'controller' => 'index',
    'action' => 'index',
));

I tested the code and it works. You should look for typos. See if you defined the namespace and controller corectly. The file name must be the same as the controller class.

thank you

I tested your suggestion, and index controller and index action can be found correctly, but if I want to access some other controllers or actions, it would be redirect to entrance module

finally I found some possible solution

$router->add("/:module/:controller/:action/:params", array( 'module' => 1, 'controller' => 2, 'action' => 3, 'params' => 4 ));

it can find correct controller and action, but if you don't assign controller or action, it can't find default index controller or index action.

so I think an improvement is to assign a customized dispatcher to recognize index