Collections and Pagination

In most use cases, you’ll not want to return a collection containing every resource in that collection; this will quickly get untenable as the number of resources in that collection grows. This means you’ll want to paginate your collections somehow, returning a limited set of resources at a time, delimited by some offset in the URI (usually via query string).

Additionally, to follow the Richardson Maturity Model properly, you will likely want to include relational links indicating the next and previous pages (if any), and likely the first and last as well (so that those traversing the collection know when to stop).

This gets tedious very quickly.

Fortunately, PhlyRestfully can automate the process for you, assuming you are willing to use Zend\Paginator to help do some of the heavy lifting.

Paginators

ZendPaginator is a general purpose component for paginating collections of data. It requires only that you specify the number of items per page of data, and the current page.

The integration within PhlyRestfully for Zend\Paginator uses a “page” query string variable to indicate the current page. You set the page size during configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
return array(
    'phlyrestfully' => array(
        'resources' => array(
            'Paste\ApiController' => array(
                // ...
                'page_size' => 10, // items per page of data
                // ...
            ),
        ),
    ),
);

All you need to do, then, is return a Zend\Paginator\Paginator instance from your resource listener (or an extension of that class), and PhlyRestfully will then generate appropriate relational links.

For example, if we consider the walkthrough example, if our onFetchAll() method were to return a Paginator instance, the collection included 3000 records, we’d set the page size to 10, and the request indicated page 17, our response would include the following links:

{
    "_links": {
        "self": {
            "href": "http://example.org/api/paste?page=17
        },
        "prev": {
            "href": "http://example.org/api/paste?page=16
        },
        "next": {
            "href": "http://example.org/api/paste?page=18
        },
        "first": {
            "href": "http://example.org/api/paste
        },
        "last": {
            "href": "http://example.org/api/paste?page=300
        }
    },
    // ...
}

Again, this functionality is built-in to PhlyRestfully; all you need to do is return a Paginator instance, and set the page_size configuration for your resource controller.

Query parameter white listing

Often when dealing with collections, you will use query string parameters to allow such actions as sorting, filtering, and grouping. However, by default, those query string parameters will not be used when generating links. This is by design, as the relational links in your resources typically should not change based on query string parameters.

However, if you want to retain them, you can.

As noted a number of times, the ResourceController exposes a number of events, and you can tie into those events in order to alter behavior. One method that the HalCollection class exposes is setCollectionRouteOptions(), which allows you to set, among other things, query string parameters to use during URL generation. As an example, consider this listener:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$allowedQueryParams = array('order', 'sort');
$sharedEvents->attach('Paste\ApiController', 'getList.post', function ($e) use ($allowedQueryParams) {
    $request = $e->getTarget()->getRequest();
    $params  = array();
    foreach ($request->getQuery() as $key => $value) {
        if (in_array($key, $allowedQueryParams)) {
            $params[$key] = $value;
        }
    }
    if (empty($params)) {
        return;
    }

    $collection = $e->getParam('collection');
    $collection->setCollectionRouteOptions(array(
        'query' => $params,
    ));
});

The above is a very common pattern; so common, in fact, that we’ve automated it. You can whitelist query string parameters to use in URL generation for collections using the collection_query_whitelist configuration parameter for your resource controller:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
return array(
    'phlyrestfully' => array(
        'resources' => array(
            'Paste\ApiController' => array(
                // ...
                'collection_query_whitelist' => array('order', 'sort'),
                // ...
            ),
        ),
    ),
);