We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Micro Routing

What would be a (good) way to define:

  1. Optional parameters. For example: /articles /articles/2013 /articles/2013/06

  2. Query parameters, that can also be optional (search, pagination, callback, defining which fields to return being the most common examples): /articles?page=2&per_page=100 /search?q=something&sort=published


98.9k

The query string is omitted by Phalcon\Mvc\Router, this means that /articles?page=2&per_page=100 is treated as just /articles.

You can set optional parameters this way:

$app->map('/articles[/]?{year:[0-9]*}[/]?{month:[0-9]*}', function($year=null, $month=null) {

});


3.6k

Thank you for taking the time to answer all those questions. Always appreciated.

I've implemented a "REST Controller" in my Micro app. All my RESTish Controllers inherit from it. It parses out search queries, pagination settings, partial responses, etc.. from every requests' query string. https://github.com/cmoore4/phalcon-rest



3.6k

@Sean Moore I greatly appreciate your generosity in sharing this.



3.6k

@Sean Moore What is the idea behind enclosing the list of key:value pairs with parenthesis?

It's purely for ease of reading. Compare: search=material:steel,source:canada,weight:5&fields=location,name,id search=(material:steel,source:canada,weight:5)&fields=(location,name,id)

The second reads a bit nicer, and will be easier for people not familiar with your API to visually parse.