简体   繁体   中英

How to work with groups in Hateoas PHP library?

I'm using Hateoas library with Symfony.

And I have a Doctrine entity and want to restrict some of the embedded content. I saw that Hateoas has exclusion for groups but I don't have any idea how to configure them and didn't found nothing about it in lib documentation. Any help would be appreciated.

If you define a serializer group in your entity

class Client
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     *
     * @JMS\Expose
     * @JMS\Groups({"default", "full"})
     */
    private $firstName;

    ...

}

You should also define it in the HATEOAS annotation (or the configuration system you're using)

 * @Hateoas\Relation("_self",
 *      href = @Hateoas\Route("api_clients_get_client", parameters = {"id" = "expr(object.getId())"}, absolute = true),
 *      exclusion = @Hateoas\Exclusion(groups={"default","full"})
 * )

This makes sense as you don't want to expose your links if you're not exposing your properties.

ie you have two groups "public" and "admin"

In admin you're likely exposing things that are hidden to a public call to your api. But if you don't have control over the HATEOAS links you're exposing in the calls, you'd be probably exposing unwanted links in your "public" call

Exclusion is covered in this section: https://github.com/willdurand/Hateoas#exclusion . You should probably rephrase your question so that we can help you in a better way.

I personally spent quite a lot of time writing this documentation, it would be great to know what is missing ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM