willdurand / BazingaHateoasBundle

Integration of the Hateoas library into Symfony.
MIT License
294 stars 59 forks source link

Register a new method in expr() #40

Closed flo-sch closed 4 years ago

flo-sch commented 9 years ago

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() ?

leroy0211 commented 8 years ago

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))"
 * ) 
 */