Routing priority parse error and defaults not set
"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)
([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]
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]
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
));
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;
}
});
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;});
Okay, but why if action not matched from the pattern, a router do not return default action
Good question!
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
@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)
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?
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)
Moreover, what if I want to use a default numeric value here, but 15 instead of 2
@phalcon Can I hope that this issue will be solved somehow? Or do you think it should stay as it is?