Created
September 25, 2014 14:06
-
-
Save weierophinney/2c47c9d59f4a5214f0c3 to your computer and use it in GitHub Desktop.
One way to return a HAL entity from an Apigility RPC controller.
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 | |
public function someAction() | |
{ | |
$data = doSomeWorkFetchSomeData(); | |
$entity = new \ZF\Hal\Entity($data, $data['id']); // or similar | |
return new \ZF\ContentNegotiation\ViewModel([ | |
'payload' => $entity, | |
]); | |
} |
Incase someone else has this problem.
You can use the Hal plugin that the Rest controllers use to generate Hal entities containing links:
<?php
public function anRpcAction()
{
$entity = $this->getEntityFromSomewhere();
$route = $this->getEvent()->getRouteMatch();
return new ViewModel([
'payload' => $this->Hal()->createEntity($entity, $route, $route->getMatchedRouteName())
]);
}
If you havn't defined a REST service for the entity you want to return, you might need to configure the metadata map
by hand
'zf-hal' => [
'metadata_map' => [
'User' => [
'route_name' => 'api.rest.user',
'entity_identifier_name' => 'username',
'route_identifier_name' => 'user_id',
'hydrator' => 'Zend\Stdlib\Hydrator\ObjectProperty',
],
],
],
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hal links are not generated using this way. Any suggestions? Thank U.
getting data