Created
November 18, 2014 21:44
-
-
Save weierophinney/c4441c925f12ea72a9ee to your computer and use it in GitHub Desktop.
Example of embedding multiple collections in a HAL payload within Apigility.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array( | |
/* ... */ | |
'zf-hal' => array( | |
'metadata_map' => array( | |
/* ... */ | |
'Api\V1\Rest\Classes\ClassesEntity' => array( | |
'entity_identifier_name' => 'name', | |
'route_name' => 'api.rest.classes', | |
'route_identifier_name' => 'class_name', | |
'hydrator' => 'Zend\Stdlib\Hydrator\ClassProperty', | |
), | |
'Api\V1\Rest\Classes\ClassesCollection' => array( | |
'entity_identifier_name' => 'name', | |
'route_name' => 'api.rest.classes', | |
'route_identifier_name' => 'class_name', | |
'is_collection' => true, | |
), | |
'Api\V1\Rest\Properties\PropertiesEntity' => array( | |
'entity_identifier_name' => 'name', | |
'route_name' => 'api.rest.properties', | |
'route_identifier_name' => 'prop_name', | |
'hydrator' => 'Zend\Stdlib\Hydrator\ClassProperty', | |
), | |
'Api\V1\Rest\Properties\PropertiesCollection' => array( | |
'entity_identifier_name' => 'name', | |
'route_name' => 'api.rest.properties', | |
'route_identifier_name' => 'prop_name', | |
'is_collection' => true, | |
), | |
/* ... */ | |
), | |
), | |
/* ... */ | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* We'll assume that all of the above classes are defined. | |
* | |
* We'll assume that Api\V1\Rpc\Query\QueryController will handle an RPC service | |
* call to /search?s=thing. It needs to be configured such that it responds to | |
* GET requests, and Content Negotiation is set to HalJson. | |
* | |
* This is what it will look like: | |
*/ | |
namespace Api\V1\Rpc\Query; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use ZF\ContentNegotiation\ViewModel; | |
use ZF\Hal\Entity; | |
class QueryController extends AbstractActionController | |
{ | |
public function queryAction() | |
{ | |
$search = $this->queryParam('s', ''); | |
/* | |
* Do some work, create a ClassesCollection $classes | |
* Do some work, create a PropertiesCollection $properties | |
* | |
* In each case, call $object->setItemCountPerPage(-1) to disable pagination, | |
* if they are paginators. (Ignore, if they are not.) | |
*/ | |
$data = [ | |
'query' => $search, | |
'classes' => $classes, | |
'properties' => $properties, | |
]; | |
$entity = new Entity($data); | |
return new ViewModel([ 'payload' => $entity ]); | |
} | |
} | |
/* The above will be rendered as a single HAL entity, with no links (unless you | |
* inject some yourself), and two embedded collections, one for classes, one | |
* for properties. Each item in each collection will be a HAL resource itself, | |
* and properly linked to its source. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment