cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Route variable duplication when use `(foo|bar)` conditions in regexp

Open deadkrolik opened this issue 13 years ago • 3 comments

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

deadkrolik avatar Apr 10 '13 07:04 deadkrolik

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 ) 

ghost avatar Apr 10 '13 14:04 ghost

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.

deadkrolik avatar Apr 10 '13 16:04 deadkrolik

You're right I'm going to add a note to the docs on that, thanks

ghost avatar Apr 10 '13 16:04 ghost