phpregexlumenlumen-routing

The route() function in Lumen with a regex route parameter return a false url


In Lumen micro framework, i created a simple route with a date as parameter (ie: /2018-02-31).

$router->get(
    '{from:\d{4}(?:-\d{1,2}){2}}',
    [
        'as' => 'date',
        function($from) use ($router) {
            return $from;
            // return route('date',['from' => $from]);
        }
    ]
);

If I return $from, it will return the date as 2018-02-31.

But, when i want to return is own route with the route() function, it returns /2018-02-31(?:-\d{1,2}){2}}

I tried with other regex for the date like [0-9]{4}-[0-9]{2}-[0-9]{2} and it doesn't work as well.


Solution

  • For now, I will just use an ugly regex like this {from:[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]} to get the date.

    I found where it crashed, though.

    In the file UrlGenerator.php is defined the route() funtion.

    $uri = preg_replace_callback('/\{(.*?)(:.*?)?(\{[0-9,]+\})?\}/', function ($m) use (&$parameters) {
        return isset($parameters[$m[1]]) ? array_pull($parameters, $m[1]) : $m[0];
    }, $uri);
    

    As you can see, it search for an content between two brackets inside the uri, so for me '{from:\d{4}(?:-\d{1,2}){2}}'. It finds the first opened bracket and stops at the first closed bracket.