HATEOAS header as string

I am building RESTful API with Yii2, but have some questions regarding HATEOAS support. Requests output pagination headers and include the HATEOAS header.

However, the HATEOAS header contains all links as one long line. It is not very helpful for the consumer. Is this the standard? Is there a way to change the format in Yii to something more usable?

HATEOAS Header

+3


source to share


1 answer


Does the following look good?

"_links": {
    "self": {
        "href": "http://localhost/users?page=1"
    },
    "next": {
        "href": "http://localhost/users?page=2"
    },
    "last": {
        "href": "http://localhost/users?page=50"
    }
}

      

If so, you can easily have such links. Make sure your data model implements the interface Linkable

and then implements the method getLinks()

:



class User extends ActiveRecord implements Linkable
{
    public function getLinks()
    {
        return [
            Link::REL_SELF => Url::to(['user/view', 'id' => $this->id], true),
        ];
    }
}

      

Serializer

will automatically add "_links"

to your answer.

More details here .

+1


source







All Articles