Respect/Rest

AbstractRoute::createUri() does not work for "/" paths.

Closed this issue · 2 comments

The createUri method allows someone to create a URI for a given route.

$myRoute = $router->get('/foobar', 'Hello World');
$myRoute->createUri(); // returns /foobar

Although for a route with the "/" path, this doesn't seem to be working:

$myRoute = $router->get('/', 'Hello Index');
$myRoute->createUri(); // returns empty string, should return "/"

The problem is the function rtrim() in file AbstractRoute, line 251 approximately.
The parameter ' /' makes you remove all '/' of string.
A possible solution:

if ($pattern == '/') {
    $pattern = rtrim($pattern);
} else {
    $pattern = rtrim($pattern, ' /');
}

More fail some tests, still can not solve. =(

This will also do the trick

$pattern = preg_replace('#(?<!^)/? *$#', '', $pattern);

Remove 0 to many spaces and a trailing slash, if slash exist but not after start. We can use the # or anything else so that we don't have to escape the \/ which we need in this expression. I waited way too long to learn regex and its your best friend as a coder, hands down. Have you made friends yet?

@MichelAraujo but well spotted, you are welcome to roll a patch for this, please.