cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Incorrect route pattern generation

Open iforp opened this issue 13 years ago • 3 comments

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}

iforp avatar Feb 27 '13 17:02 iforp

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;
    }
}

ghost avatar Feb 27 '13 18:02 ghost

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

ghost avatar Feb 27 '13 18:02 ghost

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.

iforp avatar Feb 27 '13 19:02 iforp