Comments (6)
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.
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.
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.
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.
from jsonapi.
You're welcome!
from jsonapi.
Related Issues (20)
- Sync links with the latest spec
- Possibility to remove the automated self-link
- Allow to mask internal server errors as 503 status HOT 1
- Dependency Injection (using pimple )
- Keep 'relationships' and 'meta' for included resources
- add_relation in resource.php overwrite (array relations) HOT 2
- JSONP HOT 3
- Support v1.1 of the specification
- Parse request options: sparse fields, sorting, pagination, filtering HOT 4
- Problem with Content-Type: application/vnd.api+json HOT 3
- failed to generate json: Recursion detected HOT 2
- Spec 1.1 RC 2: add support for Local Ids HOT 1
- Spec 1.1 RC 2: make it easy to add describedBy links at root level HOT 1
- Spec 1.1 RC 2/3: add support for new properties of the link object HOT 1
- Spec 1.1 RC 2: support extensions and improve support for profiles HOT 1
- Validation for received document structure
- Validation for query parameters (include, sparse fieldset, sort, page, filter)
- Allow include in included and data resourses with same id HOT 1
- Problems using ResourceDocument::fromObject
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi.