willdurand / BazingaHateoasBundle

Integration of the Hateoas library into Symfony.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Register a new method in expr()

flo-sch opened this issue · comments

I have an entity with a $filepath property,
And I have a getWebPath() method which gives the relative web path (currently, uploads/some/sub/folders/the-file-name.extension)

But instead of serializing this relative web path :

{
    "id": 1,
    "filepath": "uploads/some/sub/folders/the-file-name.extension"
}

I would prefer to serialize the absolute web one :

{
    "id": 1,
    "filepath": "http://example.com/uploads/some/sub/folders/the-file-name.extension"
}

So I was just wondering, would it be a good practice to register a new method (asset() for instance) inside expr() ?

/**
 * @Hateoas\Relation(
 *      "filepath",
 *      href = "expr(asset(object.getWebPath(), true))"
 * )
 */

Then how to register it in expr() ?

You can use the link expression from the Hateoas library and set the third parameter to true.

/**
 * @Hateoas\Relation(
 *         "filepath",
 *         href = "expr(link(object.getWebPath(), "self", true))"
 * ) 
 */