Giter Club home page Giter Club logo

Comments (6)

lode avatar lode commented on September 25, 2024

I can see logic in the suggestion @planetcrypton, but I don't really understand your situation. Do you have a code example?

Links being an array with a href property is the new (and according to spec) behavior of links (set_self_link() / add_link()). See also https://github.com/lode/jsonapi/releases/tag/v1.5.0. You can also see this in the resource links example: https://github.com/lode/jsonapi/blob/master/examples/resource_links.php:

{
    "links": {
        "self": {
            "href": "/user/42?example=true",
            "meta": {
                "level": "both"
            }
        },
    },
}

The old behavior is also still possible, and default if you don't pass extra metadata. Meaning you can for sure get a primary link which is a string.

However, for me, isset($someString['href']) returns false. Meaning $base_url will be $this->primary_links['self']. Are you sure your ['self']['href'] doesn't contain /?

from jsonapi.

planetcrypton avatar planetcrypton commented on September 25, 2024

Ok, what I was doing is using the old syntax, and didn't use add_link and thus relying on the link generated by this library.
And it that case $this->primary_links['self'] is a string. Using $this->primary_links['self']['href'] tries to cut some off that string, as if it was ...[1], look:

$x = "string";
var_dump($x['y']); // string(1) "s"

But thanks for referring to the new link syntax, and now I am using add_link(...) and have a value for ...['self']['href'].

from jsonapi.

planetcrypton avatar planetcrypton commented on September 25, 2024

I know it's a little off-topic, but could you point out for me when and where to use LINK_LEVEL_ROOT or LINK_LEVEL_DATA or LINK_LEVEL_BOTH for a collection and a single resource (and an error for that matter) response..??
I'm sort of confused...

from jsonapi.

lode avatar lode commented on September 25, 2024

Thanks, I tried with less strict error handling (I tried with Whoops before and that just takes over on every php error). I now used this:

ini_set('display_errors', 1);
error_reporting(-1);

$x = "string";
var_dump(isset($x['y'])); // false
var_dump($x['y']); // Warning: Illegal string offset 'y' + string(1) "s"

So reproducing the cutting of the string. But also, when using isset() it returns false, thus

$base_url = (isset($this->primary_links['self']['href'])) ? $this->primary_links['self']['href'] : $this->primary_links['self'];

should be equal to $base_url = $this->primary_links['self'];.


The internal link creation is a bit broken. I plan to fix and improve flexibility in that area some time. For now it is indeed best to manually add links if you want them to be correct.


The link levels are indeed a bit confusing in jsonapi :)

When using add_link() on a collection or errors object the links will end up in the root links element. Thus $collection->add_link('spec', 'http://jsonapi.org') would result in:

{
    "links": {
        "spec": "http://jsonapi.org"
    }
}

The levels you mention (root/data/both) are used for adding links to resources. This is because on resources links can exist on the same (root) level as for collections links, as well as on the data level data.links. With the level you can choose where the link you're adding will end up at, root, data, or both.

Thus using LINK_LEVEL_ROOT on a resource would have the same effect as adding links on a collection or errors object. Using LINK_LEVEL_DATA would result in:

{
    "data": {
        "links": {
            "spec": "http://jsonapi.org"
        }
    }
}

And using LINK_LEVEL_BOTH in the combination of them. You can also see these in practice in https://github.com/lode/jsonapi/blob/master/examples/resource_links.php.

Conceptually the difference is that links at root level are more about the endpoint (e.g. spec, or next), and links at data level are more about the resource (e.g. profile, twitter of a user).

Does this help you?

from jsonapi.

planetcrypton avatar planetcrypton commented on September 25, 2024

from jsonapi.

lode avatar lode commented on September 25, 2024

You're welcome!

from jsonapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.