Hateoas Bundle
While this works as a simple, drop in bundle for Symfony, you can find a much more full featured library and bundle here:, (*1)
Overview
The Hateoas Bundle works with JMS Serializer to allow you to easily add Hateoas
compliant resource urls to the JSON output of your REST Api., (*2)
There are other similar bundles, but they seemed heavy for my needs. This bundle
was designed to work seamlessly with JMS Serializer,
with out needing to abstract or obfuscate the serialization of your data., (*3)
Currently this bundle only provides Annotations for Resource Linking in your
Serialized response., (*4)
Hateoas Bundle Installation
The best method of installation is through the use of composer., (*5)
Add the bundle to Composer
"require": {
"kmfk/hateoas-bundle": "~0.1",
}
Update AppKernel.php
Add The Hateoas Bundle to your kernel bootstrap sequence, (*6)
public function registerBundles()
{
$bundles = array(
// ...
new Kmfk\Bundle\HateoasBundle\HateoasBundle(),
);
return $bundles;
}
The bundle allows you to configure the Rest API host and an optional Path prefix.
Your links will be built using these values. If they are not set, the bundle will
default to parsing this from the current request., (*7)
#app/config.yml
hateoas:
host: http://api.example.com/
prefix: /api/
Annotations
Once configured, you can use the Annotations provided by this bundle to easily
add resource links to your classes and properties., (*8)
#src/AcmeBundle/Entity/UserEntity.php
use Kmfk/Bundle/HateoasBundle/Annotation/Hateoas;
/**
* @Hateoas(
* name ="self",
* href ="/user/{id}/"
* params ={"id" = "getId"},
* groups ={"list"},
* type ="absolute"
* )
*/
class UserEntity
{
protected $id;
public function getId()
{
return $this->id;
}
}
Output:
{
"_links": {
"self": {
"href": "http://api.example.com/api/user/1/"
}
}
}
Annotation Reference
| Property |
Description |
Example |
name |
The property name inside the 'links' attribute |
user |
href |
The relative (path) url of the resource, including url tokens |
/user/{id}/ |
params |
An associative array of token names with their corresponding getter methods |
{ "id" = "getId" } |
groups |
Serializer Groups, Used the same way as JMS Serializer Groups |
{ "partial", "full" } |
type |
'Absolute' or 'Embedded' |
absolute |
Using Params
You can have multiple tokens in the href. The params array should be an associative array
with keys matching the tokens in the path. Methods listed should be methods that exist in the
annotated class., (*9)
Groups
Specifying groups allow you to control the output of the links based on
Exclusion Groups, (*10)
Embedded vs Absolute Links
While absolute (default value), will allows include the API Host and optional prefix,
embedded urls live beneath another resource. Setting type to 'embedded will allow you
to have links like:, (*11)
{
"_links": {
"self": {
"href": "http://api.example.com/api/user/1/email/1/"
}
}
}