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

Routing from within a module

Is there an example of the "proper" way to approach routing if you want a module to implement its own routes?

I would like to use the multi module architecture approach when working with Phalcon, but I'm having a difficult time finding examples of how to approach this in terms of the module defining its own routes, including the bootstrap picking them up.

edited Mar '14

My apps 'modularity' is structuarally a bit different to the 'official' one but that's how I've done it: (which may give you some hints as to how to do your's)

$routes = $config->routes;
$di->set('router', function() use($routes) {

    $router = new Router(false);

    $router->removeExtraSlashes(true);

    foreach ($routes as $pattern => $path) {
        $router->add($pattern, $path->toArray());
    }

    return $router;
});

I keep routes in a module's config file so $config holds an instance of Phalcon\Config

  • example of such config entry would be:
'routes' => array(
        '/:module/:controller/:action/:params' => array(
            'module'     => 1,
            'controller' => 2,
            'action'     => 3,
            'params'     => 4,
        ),
)