Links¶
HAL defines two specific structures: _links
and _embedded
. This
page discusses using links, self-referential links, related links, and
complex links. For pagination links, see collections.
Links are URLs to URL resources (not to be confused with a HAL resource
class). You may create any number of uniquely named links for each HAL
Resource. The only strongly-suggested link is the self
link.
Self Link¶
"_links":{
"self":{
"href": "http://website.com/customer/1"
}
},
Every HAL resource should include a self
link. The self
link gives
the URL to the current resource. This is not just the current requested url:
in a collection of resources each resource self
link is a link to that
individual resource.
Every hydrator resource should assign a self
link.
return $this->hydratorManager->resource($data)
->addLink('self', route('routeName', $class->id));
Complex Link¶
By default, when you add a new link, the link is added to the href property of
the link. However the HAL specification allows for multiple properties and even
arrays of objects. For this reason you may pass an array as a second parameter
to addLink
. The array will be rendered exaclty as it was assigned.
Note
This is included for completeness and it is rare to use this feature.
->addLink('ea:find', ['href' => '/orders{?id}', 'templated' => true]);
The special name curies
, see curie syntax
allows for arrays of link data:
->addLink('curies', [['name' => 'ea', 'href' => 'http://example.com/docs/rels/{rel}', 'templated' => true]]);
This is documentation for Hypertext Application Language for Laravel. If you find this useful please add your ★ star to the project.