cphalcon
cphalcon copied to clipboard
Incorrect route pattern generation
echo Phalcon\Version::get(); // 0.9.1
$routes = array(
'/admin',
'/admin/',
'/admin/asd',
);
$router = new Phalcon\Mvc\Router();
$router->add('/admin[/]{0,1}');
$router->add('/admin/:controller[/]{0,1}');
foreach ($routes as $route)
{
$router->handle($route);
echo $router->getMatchedRoute()->getPattern(), '<br>';
}
Expected output:
/admin[/]{0,1}
/admin[/]{0,1}
/admin/:controller[/]{0,1}
In fact output:
#^/([a-zA-Z0-9\_]+)[/]{0,1}$#
#^/([a-zA-Z0-9\_]+)[/]{0,1}$#
/admin/:controller[/]{0,1}
It must be as follows:
$routes = array(
'/admin',
'/admin/',
'/admin/asd',
);
//Create the router resetting default routes
$router = new Phalcon\Mvc\Router(false);
$router->add('/admin([/]{0,1})'); // Enclose regular expression parts in ( )
$router->add('/admin/:controller([/]{0,1})'); // Enclose regular expression parts in ( )
foreach ($routes as $route)
{
$router->handle($route);
if ($router->wasMatched()) {
echo $router->getMatchedRoute()->getPattern(), PHP_EOL;
}
}
Also, to avoid those regular expressions to deal with extra slashes you could use the buit-in functionality for that: http://docs.phalconphp.com/en/latest/reference/routing.html#dealing-with-extra-trailing-slashes
Thanks! I followed the documentation:
Or, you can modify specific routes to optionally accept trailing slashes:
$router->add(
"/{language:[a-z]{2}}/:controller[/]{0,1}",
array(
"controller" => 2,
"action" => "index"
)
);
I think it would be useful to add brackets to the documentation.