Hi
I'm trying to implement optional route parameters in a Micro application.
A while back when using Slim, I was able to define optional params like so;
$app->get('/search(/:year(/:month(/:day)))', function($year=0, $month=0, $day=0) {
echo sprintf('%s-%s-%s', $year, $month, $day);
})->conditions(array('year'=>'(19|20)\d\d', 'month'=>'0[1-9]|1[0-2]', 'day'=>'0[1-9]|1[0-9]|2[0-9]|3[0-1]'));
which will match /search, /search/2015, /search/2015/09 and /search/2015/09/30, and set missing parameters to 0 as per usual php default value syntax.
I'm trying to do the same in a Micro app, and after searching the discussions have come up with this;
$app->get('/search/?{year:([0-9]{4})?}/?{month:([0-9]{2})?}/?{day:([0-9]{2})?}', function($year=0, $month=0, $day=0) {
echo "{$year} - {$month} - {$day}";
});
this also matches /search, /search/2015, /search/2015/09 and /search/2015/09/30 as my slim route did, however, it seems to be setting missing parameters to their numeric index (or pattern match index?), instead of defaulting to 0; e.g.
route | echoed result |
---|---|
/search | 1 - 2 - 3 |
/search/2015 | 2015 - 2 - 3 |
/search/2015/09 | 2015 - 09 - 3 |
/search/2015/09/30 | 2015 - 09 - 30 |
I can't see where these 'default' values (1 2 3) are coming from, unless my regex is introducing these.
Thanks