Created
May 20, 2014 15:38
-
-
Save ezimuel/e2b528ba160197d0c773 to your computer and use it in GitHub Desktop.
RPC with HAL JSON
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 | |
namespace Test\V1\Rpc\Test; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use ZF\ContentNegotiation\ViewModel; | |
use ZF\Hal\Entity; | |
use ZF\Hal\Link\Link; | |
class TestController extends AbstractActionController | |
{ | |
public function testAction() | |
{ | |
$id = 1; | |
$data = array ( 'name' => 'Enrico Zimuel', 'id' => $id ); | |
$entity = new Entity($data, $id); | |
$entity->getLinks()->add(Link::factory(array( | |
'rel' => 'contacts', | |
'route' => array( | |
'name' => 'test.rest.user', | |
) | |
))); | |
return new ViewModel(array( | |
'payload' => $entity | |
)); | |
} | |
} |
I think we can add this information in this recipe https://www.apigility.org/documentation/recipes/hal-from-rpc, what do you think?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ezimuel In reading up on HAL and looking through our Hal plugin, two things:
The "self" relational link is not strictly required, just strongly recommended (a SHOULD in IETF parlance, but not a MUST). As such, we do not, and should not, inject a self relational link for entities created in RPC controllers, and instead leave it up to the user whether or not they want to inject one.
As to that latter point, this can be accomplished using the HAL plugin's
injectSelfLink()
method:This will use the provided route name and route identifier name to create the self relational link.
Alternately, another approach is to use the plugin's
createEntity()
method. This takes the object you're exposing, plus the route and route identifier names, and returns aZF\Hal\Entity
instance with the self relational link injected:So, now the question is: where should we put this in the manual! :)