Skip to content
Remy edited this page Nov 8, 2021 · 8 revisions

According to JSON API Links Specification link is an object that might be a URL string or an object with meta information. Examples of both are shown below

"links": {
    "self": "http://example.com/articles/1",
    "related": {
        "href": "http://example.com/articles/1/comments",
        "meta": {
            "count": 10
        }
    }
}

Links could be added to various parts of JSON API Document e.g. top level, resources, relationships, included resources (many samples could be found in the specification).

Class Link supports both representations as a URL string and as an object. That's the Link constructor declaration

/**
 * @param bool   $isSubUrl If $value is a sub-url and encoder should add a URL prefix to it.
 * @param string $value    URL value.
 * @param bool   $hasMeta  If links has meta information.
 * @param mixed  $meta     Meta information.
 */
public function __construct(bool $isSubUrl, string $value, bool $hasMeta, $meta = null)

Typically $value is specified as a part URL like /authors and will be automatically merged with other parts e.g. prefixes, paths to resources, resource identifies, etc. However you might want to avoid such behaviour and show URLs without any prefixing. In order to do that you need to set $isSubUrl to false.

As you can see LinkInterface has some predefined constants. Here is the full list

  • SELF
  • RELATED
  • FIRST
  • LAST
  • NEXT
  • PREV
  • ABOUT

You are not limited to these constants and can use any valid json string values as link keys.

Class LinkWithAliases has an extra parameter to support JSON API aliases feature.

Also you don't have to use Link but LinkInterface could be used instead if you need custom behaviour such as usage application specific objects within its methods.

Clone this wiki locally