Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HATEOAS header as a string

I am building a RESTful API with Yii2 but have some questions in regards to HATEOAS support. Requests will output the pagination headers and include the HATEOAS header.

However the HATEOAS header contains all links as one long string. This is not very helpful for the consumer. Is this standard? Is there a way to change the format in Yii into something that is easier to handle?

HATEOAS Header

like image 278
Dubby Avatar asked Dec 04 '25 04:12

Dubby


1 Answers

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 links like that. Make sure your data model implements Linkable interface and then implement getLinks() method:

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 response.

More info here.

like image 112
Beowulfenator Avatar answered Dec 06 '25 17:12

Beowulfenator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!