Defining optional parameters in a Micro Route

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



83

Did you find a solution or easy workaround for optional parameters using Phalcon Micro?

Did you find a solution or easy workaround for optional parameters using Phalcon Micro?

To do it in a single route like shown, the regex correctly matches up the route, but optional missing params are still passed through as what looks like their index.

So potentially, I could check in the function if year, month, or day are single digit integers then act like that parameter wasn't passed.

Alternatively, I could just define 3 routes.

So there are workarounds, I just think Phalcon shouldn't be inserting values for parameters that have no match.