Default route for each module

Hi

I have some route groups for my modules:

$di['router'] = function () {

    $router = new Router();

    $router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI);
    $router->removeExtraSlashes(true);
    //$router->setDefaultModule("main");
    //$router->setDefaultNamespace("Myapp\Main\Controllers");

    $routerMain = new \Phalcon\Mvc\Router\Group(array(
        'module' => 'main',
        'namespace' => "Myapp\Main\Controllers",
        'controller' => 'index'
    ));
    $routerMain->setHostName('example.com');
    $routerMain->add('/', array(
        'action' => 'index'
    ));
    $routerMain->add('/add', array(
        'action' => 'add'
    ));

    $routerTravel = new \Phalcon\Mvc\Router\Group(array(
        'module' => 'travel',
        'namespace' => "Myapp\Travel\Controllers",
        'controller' => 'index'
    ));
    $routerTravel->setHostName('travel.example.com');
    $routerTravel->add('/', array(
        'action' => 'index'
    ));
    $routerTravel->add('/add', array(
        'action' => 'add'
    ));

    $router->mount($routerMain);
    $router->mount($routerTravel);

    return $router;
};

All routes are working perfectly. But I want to add a default route

/:controller/:action/:params

to all groups with their own modules and namespaces.

If I set

$router->setDefaultModule("main");
$router->setDefaultNamespace("Myapp\Main\Controllers");

this applies to module "Main" to all groups.

I only see one way to do id - write this in each group:

$routerGroup->add('/:controller', array(
    'controller' => 1
));

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

$routerGroup->add('/:controller/:action/:params', array(
    'controller' => 1,
    'action' => 2,
    'params' => 3
));

Am I right?



82.3k

These routes are already part of the default routes:

$routerGroup->add('/:controller', array(
    'controller' => 1
));

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

$routerGroup->add('/:controller/:action/:params', array(
    'controller' => 1,
    'action' => 2,
    'params' => 3
));

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/router.zep#L108

edited Jul '14

Yes, but I have to add these routes to all my groups. In my project I have 12-15 modules. 12-15 router groups with own module, namespace and hostname. So there is a way to add universal default route that will run the module controller depending on the group?