Route variable duplication when use `(foo|bar)` conditions in regexp
All url conditions in routes after first don't assign variables in controller method correctly. For more info see gist https://gist.github.com/deadkrolik/8f8718b3aec6edd05f31
Actually, the problem are the ( ), because Phalcon already adds them, so it creates a double subpattern:
// '#^/correct_complex/([a-z]+)/((abc|qwe))$#
$router->add('/correct_complex/{var1:[a-z]+}/{var2:(abc|qwe)}', array(
'controller' => 'index',
'action' => 'routeOne',
));
// '#^/incorrect_complex/((aaa|bbb))/((abc|qwe))$#'
$router->add('/incorrect_complex/{var1:(aaa|bbb)}/{var2:(abc|qwe)}', array(
'controller' => 'index',
'action' => 'routeOne',
));
The correct routes are those without the parentheses:
// '#^/correct_complex/([a-z]+)/(abc|qwe)$#
$router->add('/correct_complex/{var1:[a-z]+}/{var2:abc|qwe}', array(
'controller' => 'index',
'action' => 'routeOne',
));
// '#^/incorrect_complex/(aaa|bbb)/(abc|qwe)$#'
$router->add('/incorrect_complex/{var1:aaa|bbb}/{var2:abc|qwe}', array(
'controller' => 'index',
'action' => 'routeOne',
));
If you run that incorrect pattern with preg_match you'll see how subpatterns are duplicated:
preg_match('#^/incorrect_complex/((aaa|bbb))/((abc|qwe))$#', '/incorrect_complex/aaa/abc', $matches);
print_r($matches);
Producing:
Array ( [0] => /incorrect_complex/aaa/abc [1] => aaa [2] => aaa [3] => abc [4] => abc )
Thank you. I've understood. But I think this behaviour should be described in documentation, because it's not obvious. We use:
$router->add('/api/(aaa|bbb)', array(
));
But in this example, when we need a parameter name we should use:
$router->add('/api/{parameter:aaa|bbb}', array(
));
And we don't need the parentheses.
You're right I'm going to add a note to the docs on that, thanks