flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.64k stars 407 forks source link

Optional Parameters #361

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hi, I am having issues with Optional Parameters. I may not be using them correctly so please let me know if there is another way i can accomplish this.

This works: /api/itil/filter/12/orderby/8/ascending/1/page/2 (without /results/@ResultsPerPage)

But this does not work: /api/itil/filter/12/page/12

Here is my route code:

Flight::route('GET /api/itil/filter/@Id(/orderby/@OrderById)(/ascending/@OrderByAsc)(/page/@PageNo)(/results/@ResultsPerPage)', function ($Id, $OrderById = null, $OrderByAsc = null, $PageNo = null, $ResultsPerPage = null) {

});
mikecao commented 6 years ago

Optional parameters have to wrap completely like in the example

Flight::route('/blog(/@year(/@month(/@day)))', function($year, $month, $day){
    // This will match the following URLS:
    // /blog/2012/12/10
    // /blog/2012/12
    // /blog/2012
    // /blog
});

You can have them next to each other like (@year)/(@month). Also, when designing your api, it's better to keep query parameters in the query and not the url. The url should just represent the object. So /api/users?orderBy=name&sort=desc instead of /api/users/orderBy/name/sort/desc