I am following Modx rest api documentation and video tutorials in youtube.
And I am stuck in Pagination thing with api. I want to know/learn how to get pagination via calling in json ?
I want to fetch only specific number of resources in json. Link 5 or 10 at once instead of all.
currently we get all resources with this call -
http://localhost/modx-all-test-1/rest/resources/
and only specific resource by putting id -
http://localhost/modx-all-test-1/rest/resources/1
here 1 is the id of resource.
I dont want to fetch all resources in my android app. I want to fetch only first 5 or 10 resources…
In wordpress, we get this by following example call -
/ wp/v2/posts?per_page=5’
I want per page pagination in Rest APi json data.
Also I want to fetch collections Extra’s child resources.
And tags as for my app for which iam using tagger in blogIt extra.
My knowledge is low and I need help to achieve this milestone.
My code is -
class MyControllerResources extends modRestController {
public $classKey = ‘modResource’;
// public $defaultSortField = ‘sortorder’;
public $defaultSortDirection = ‘ASC’;
// [‘tvvalue’] = $object->getTVValue(‘tvname’);
public $defaultSortField = ‘id’;
// public $defaultSortDirection = ‘DESC’;
public function prepareListObject(xPDOObject $object) {
$data = array();
$data['pagetitle'] = $object->pagetitle;
$data['content'] = $object->content;
$data['introtext'] = $object->introtext;
$data['longtitle'] = $object->longtitle;
$data['description'] = $object->description;
$data['featuredimage'] = $object->getTVValue('myimagetv');
$data['kitnelog'] = $object->getTVValue('mysimpletexttv');
return $data;
}
public function read($id) {
if (empty($id)) {
return $this->failure($this->modx->lexicon('rest.err_field_ns',array(
'field' => $this->primaryKeyField,
)));
}
// @var xPDOObject $object
$c = $this->getPrimaryKeyCriteria($id);
$this->object = $this->modx->getObject($this->classKey,$c);
if (empty($this->object)) {
return $this->failure($this->modx->lexicon('rest.err_obj_nf',array(
'class_key' => $this->classKey,
)));
}
$objectArray = $this->object->toArray();
$objectArray += array(
'featuredimage' => $this->object->getTVValue('myimagetv'),
'kitnelog' => $this->object->getTVValue('mysimpletexttv')
);
unset(
$objectArray['alias'],
$objectArray['alias_visible'],
$objectArray['parent'],
$objectArray['type'],
$objectArray['contentType'],
$objectArray['link_attributes'],
$objectArray['published'],
$objectArray['pub_date'],
$objectArray['isfolder'],
// $objectArray['introtext'],
$objectArray['richtext'],
$objectArray['template'],
$objectArray['menuindex'],
$objectArray['searchable'],
$objectArray['cacheable'],
$objectArray['createdby'],
$objectArray['createdon'],
$objectArray['editedby'],
$objectArray['editedon'],
$objectArray['deleted'],
$objectArray['deletedon'],
$objectArray['deletedby'],
$objectArray['published'],
$objectArray['unpub_date'],
$objectArray['publishedon'],
$objectArray['publishedby'],
$objectArray['menutitle'],
$objectArray['donthit'],
$objectArray['privateweb'],
$objectArray['privatemgr'],
$objectArray['content_dispo'],
$objectArray['hidemenu'],
$objectArray['class_key'],
$objectArray['context_key'],
$objectArray['content_type'],
$objectArray['uri'],
$objectArray['uri_override'],
$objectArray['hide_children_in_tree'],
$objectArray['show_in_tree'],
$objectArray['properties']
);
$afterRead = $this->afterRead($objectArray);
if ($afterRead !== true && $afterRead !== null) {
return $this->failure($afterRead === false ? $this->errorMessage : $afterRead);
}
return $this->success('',$objectArray);
}
}