cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Routing priority parse error and defaults not set

Open 3axap4eHko opened this issue 13 years ago • 13 comments

"Exception: 'Action '3' was not found" when router parse Uri, but expression ([a-zA-Z][a-zA-Z0-9_-]+) not match for "31"

uri: /api/rest/v1/index/31/ pattern: #^/api/rest/v1/([a-zA-Z][a-zA-Z0-9_-]+)/?(\d+)?/?([a-zA-Z][a-zA-Z0-9_-]+)?/?$# paths: 'module' => 'Api' 'controller' => 1 'id' => 2 'action' => 3 _defaultAction: 'index'

Сalling preg_match function for this pattern will return: array (size=3) 0 => string '/api/rest/v1/index/31/' (length=22) 1 => string 'index' (length=5) 2 => string '31' (length=2)

3axap4eHko avatar Mar 22 '13 14:03 3axap4eHko

([a-zA-Z][a-zA-Z0-9_-]+) can't match 31 because the pattern explicitly sets that the value must start with a letter [a-zA-Z]

ghost avatar Mar 22 '13 15:03 ghost

You did not understand me, why router trying match action to 31? This is second subpattern and second subpattern in "paths" variable is "id", and pattern ([a-zA-Z][a-zA-Z0-9_-]+) can NOT match to 31 because value MUST start with a letter [a-zAZ]

3axap4eHko avatar Mar 22 '13 16:03 3axap4eHko

This is caused due to the optional patterns in the regular expression:

preg_match('#^/api/rest/v1/([a-zA-Z][a-zA-Z0-9_-]+)/?(\d+)?/?([a-zA-Z][a-zA-Z0-9_-]+)?/?$#', '/api/rest/v1/index/31/', $matches);

Produces:

Array ( [0] => /api/rest/v1/index/31/ [1] => index [2] => 31 )

As you can see, the path in the position 3 (action) is missing, so the router passes the value 3 to the dispatcher but it isn't a valid action.

You can avoid this behavior by using strict routes:

$router->add('#^/api/rest/v1/([a-zA-Z][a-zA-Z0-9_-]+)$#', array(
    'controller' => 1,  
));

$router->add('#^/api/rest/v1/([a-zA-Z][a-zA-Z0-9_-]+)/(\d+)$#', array(
    'controller' => 1,
    'id' => 2
));

$router->add('#^/api/rest/v1/([a-zA-Z][a-zA-Z0-9_-]+)/(\d+)/([a-zA-Z][a-zA-Z0-9_-]+)$#', array(
    'controller' => 1,
    'id' => 2,
    'action' => 3
));

ghost avatar Mar 22 '13 16:03 ghost

Or apply a convertion to the path if it is 3 or integer:

$router->add('#^/api/rest/v1/([a-zA-Z][a-zA-Z0-9_-]+)/?(\d+)?/?([a-zA-Z][a-zA-Z0-9_-]+)?/?$#', array(
    'controller' => 1,
    'id' => 2,
    'action' => 3
))->convert('action', function($action){
    if ($action==3) {
        return null;
    }
});

ghost avatar Mar 22 '13 16:03 ghost

I'm convinced that the router should return NULL if parameter is an integer. Any parameter can't be an integer, it's always a string, so every convert in such case is much fuss.

My app has a more complex regEex, and I have to use multiple redundant convert

$router->add(
//...
)->convert('section', function($param){return is_int($param)?null:$param;})
 ->convert('filter',  function($param){return is_int($param)?null:$param;})
 ->convert('page',    function($param){return is_int($param)?null:$param;})
 ->convert('id',      function($param){return is_int($param)?null:$param;})
 ->convert('action',  function($param){return is_int($param)?null:$param;});

iforp avatar Mar 25 '13 17:03 iforp

Okay, but why if action not matched from the pattern, a router do not return default action

3axap4eHko avatar Mar 27 '13 12:03 3axap4eHko

Good question!

roman-kulish avatar Mar 30 '13 11:03 roman-kulish

I added a fix in 1.0.1 to avoid passing integers in the module, namespace, controller and action paths, this must help avoiding the convert stuff in the routes. thanks

ghost avatar Mar 30 '13 16:03 ghost

@phalcon what about other parameters?

simple example


$router->add("/:controller/?([0-9]+)?", [
    'id' => 2
])->convert('id', function($id){
    var_dump($id);
});

$router->handle('/news');   // int 2
$router->handle('/news/2'); // string '2' (length=1)

iforp avatar Apr 01 '13 13:04 iforp

I'm not sure what to do here, if the user wants to use a default numeric value there, or want to get a null?

ghost avatar Apr 02 '13 20:04 ghost

That's the issue. It's impossible to get NULL by default. The only way is to use convert method (like in my comment https://github.com/phalcon/cphalcon/issues/502#issuecomment-15408951)

iforp avatar Apr 05 '13 14:04 iforp

Moreover, what if I want to use a default numeric value here, but 15 instead of 2

iforp avatar Apr 05 '13 14:04 iforp

@phalcon Can I hope that this issue will be solved somehow? Or do you think it should stay as it is?

iforp avatar Apr 08 '13 07:04 iforp