Use URL Parameters to Filter or Sort Data

Question

How can you pass parameters in the URL to filter data when requesting a collection?

Answer

Collection Query String whitelist under Content Negotiation in the Admin UI, or its analogue collection_query_whitelist key within api-tools-rest in module.config.php, whitelists query string arguments, allowing their value to be received by your REST resource's fetchAll($params = []) method.

This can be useful if you need to allow visitors to filter or search the collection, such as:

  • http://localhost:8000/autocomplete?state=mis
  • http://localhost:8000/books?title=php&sort=year

Whitelisting arguments also allows api-tools-rest to automatically include those parameters in the self, first, last, prev and next hypermedia links when returning a collection.

See the "Sub-key: collection_query_whitelist (optional)" section of the api-tools-rest documentation for more information on how to use collection_query_whitelist.

Images in the documentation, and the API Tools Admin UI itself, still refer to Apigility. This is due to the fact that we only recently transitioned the project to its new home in the Laminas API Tools. Rest assured that the functionality remains the same.