...
Step 1: Create PostSearchForm class to control search optionsoptions
PostSearchForm.php
Code Block |
---|
<?php namespace Apps\Posts\Api\Form; use Apps\Core_MobileApi\Api\Form\SearchForm; class PostSearchForm extends SearchForm { public function getSortOptions() { $sortOptions = parent::getSortOptions(); $sortOptions[] = [ 'value' => 'is_featured', 'label' => 'Is Featured' ]; return $sortOptions; } public function getWhenOptions() { return parent::getWhenOptions(); } } |
...
Code Block |
---|
<?php namespace Apps\Posts\Api\Service; use Apps\Core_MobileApi\Adapter\MobileApp\MobileApp; use Apps\Core_MobileApi\Adapter\MobileApp\MobileAppSettingInterface; use Apps\Core_MobileApi\Adapter\MobileApp\Screen; use Apps\Core_MobileApi\Adapter\Utility\UrlUtility; use Apps\Core_MobileApi\Api\AbstractResourceApi; use Apps\Posts\Api\Form\PostForm; use Apps\Posts\Api\Form\PostSearchForm; use Apps\Posts\Api\Resource\PostResource; class PostApi extends AbstractResourceApi implements MobileAppSettingInterface { public function __naming() { return [ 'post/search-form' => [ 'get' => 'getSearchForm' ] ]; } public function getSearchForm() { $form = $this->createForm(PostSearchForm::class, [ 'title' => 'search', 'method' => 'GET', 'action' => UrlUtility::makeApiUrl('post') ]); return $this->success($form->getFormStructure()); } /* other code... */ } |
Explain the code above:
- Method __naming() of PostApi class allows defining more API routes, Route to Search form MUST follow standard "{resource_name}/search-form"
- Method getSearchForm() handles API request and returns the response with the form's structure in JSON
...