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

Router default for named parameters at the end of the route

Using named parameters on the standard router I get a default value equal to the position, when it's not defined:

    $router->addGet("/api/:module/([a-zA-Z0-9_-]+)(/json|/csv)*", array(
        'module' => 1,
        'controller' => 'index',
        'action' => 'view',
        'element' => 2,
        'type' => 3,
        'api' => true
    ));

api/user/1 ==> 3 // I don't want the 3 to be shown, the parameter is not set

api/user/1/json ==> '/json'

using

$dispatcher->getParam('type')

The problem comes when I try to parse the "type" parameter, I must check it vs the value 3, but in other routes I will have it as the second parameter so....

How can I accomplish this?

edited May '15

Wouldn't it be better to assign parameter order using the non-associative array? I mean, the fist position is the 1, etc This way we can override the values for each position, or just let them be defined.

    $router->addGet("/api/:module/([a-zA-Z0-9_-]+)(/json|/csv)*", array(
        'module',
        'element' ,
        'type',
        'controller' => 'index',
        'action' => 'view',
        'api' => true
    ));

Also, when I use ":params" they are setted from 0 so they override whatever I use as first parameter defined. For example, using this router:

    $router->addGet("/api/:module/([a-zA-Z0-9_-]+)/:params", array(
        'module' => 1,
        'controller' => 'index',
        'action' => 'view',
        'element' => 2,
        'params' => 3,
        'api' => true
    ));

with this Action:

    public function viewAction($user_id)
    {
        print_r($this->dispatcher->getParams());
    }

with "/api/user/1"

Array
(
    [element] => 1 // assigned to my $user_id in the action method
    [api] => user
)

with "/api/user/1/json"

Array
(
    [0] => json   // assigned to my $user_id in the action method wich is bad 
    [element] => 1
    [api] => user
)

also, I don't know why but the "api" value is bad in every one of them

Any information about these? What really bothers me is the parameters getting the assigned position as default value.