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

Problem with routes in multi modules app

I have multi models application. In my Bootrstrap I set up to DI Roter class

$this->getDi()->set('router', function () {

    $router =  new \Phalcon\Mvc\Router(false);
    $router->add("/:module/:controller/:action/:params", array(
        "module" => 1, 
        "controller" => 2,
        "action" => 3,
        "params" => 4,
    ));

    $router->add('/', array(
        'namespace' => 'Frontend\Controllers',
        'module' => 'frontend',
        'controller' => 'index',
        'action' => 'test'
    ));

    return $router;
}, true);

In my module in Module.php file I add additional routes:

$router = $di->get('router'); 
$router->add("/test", array(
        'module' => 'frontend',
        'controller' => 'index',
        'action' => 'test'
));

and unfortunately no routes which were added to Module.php are working.



98.9k
Accepted
answer
edited Aug '14

Adding routes to Module.php would not work that way. Module.php is executed after parsing the current route in the router, so you will be adding routes at a stage were the module is already processed.

  • Mvc\Router identifiies module, controller and action to be executed based on current URI
  • Mvc\Router passes this parameters to Mvc\Application
  • Mvc\Application loads Module.php and executes registerAutoloader/registerServices on there (Mvc\Router already did its job!)


12.8k

Thanks for quick answer.