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-module Flexible Routing with short syntax

OS: Ubuntu 14.04 Server: nginx/1.4.6 (Ubuntu) Php: 5.5.9-1ubuntu4.6 (fpm-fcgi) Phalcon: 1.3.4

$testRoutes = array(
    '/',
    '/account',
    '/account/register',
    '/products/show/101',
);

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

$router->add("(/?)", "frontend::index::index"); //works
$router->add("/:controller/?", "frontend::1::index"); //works
$router->add("/:controller/:action/?", "frontend::1::2"); //fails with Controller: '', Action: ''
$router->add("/:controller/:action/:params/?", "frontend::1::2::3"); //fails with Controller: '', Action: ''

//Testing each route
foreach ($testRoutes as $testRoute) {
    $router->handle($testRoute);

    echo 'Testing ', $testRoute, PHP_EOL;

    if ($router->wasMatched()) {
        echo 'Controller: ', $router->getControllerName(), PHP_EOL;
        echo 'Action: ', $router->getActionName(), PHP_EOL;
    }
    echo PHP_EOL;
}

This isn't breaking behaviour, as it can be done with the array syntax, but one would assume this would work?

Thanks.

Can you try in this order

$router->add("/:controller/:action/:params/?", "frontend::1::2::3"); //fails with Controller: '', Action: ''
$router->add("/:controller/:action/?", "frontend::1::2"); //fails with Controller: '', Action: ''
$router->add("/:controller/?", "frontend::1::index"); //works
$router->add("(/?)", "frontend::index::index"); //works

More to less specific

$testRoutes = array(
    '/',
    '/account',
    '/account/register',
    '/account/register/101',
);

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

$router->add("/:controller/:action/:params/?", "frontend::1::2::3"); //fails with Controller: '', Action: ''
$router->add("/:controller/:action/?", "frontend::1::2"); //works
$router->add("/:controller/?", "frontend::1::index"); //works
$router->add("(/?)", "frontend::index::index"); //works

//Testing each route
foreach ($testRoutes as $testRoute) {
    $router->handle($testRoute);

    echo 'Testing ', $testRoute, PHP_EOL;

    if ($router->wasMatched()) {
        echo 'Module: ', $router->getModuleName(), PHP_EOL;
        echo 'Controller: ', $router->getControllerName(), PHP_EOL;
        echo 'Action: ', $router->getActionName(), PHP_EOL;
        echo 'Params: ';
        print_r($router->getParams());
    }
    echo PHP_EOL;
}

//output:
Testing /
Module: frontend
Controller: index
Action: index
Params: Array
(
)

Testing /account
Module: frontend
Controller: account
Action: index
Params: Array
(
)

Testing /account/register
Module: frontend
Controller: account
Action: register
Params: Array
(
)

Testing /account/register/101
Module:
Controller:
Action:
Params: Array
(
)

Very strange.