2017-02-14 46 views
2

我做了使用Laravel與Laravel 5 JSON API Transformer package這是上市jsonapi.orgJSON API:正確顯示鏈接的方式?

現在一個JSON API,一切正常,從我的API的示例響應有點像(順便說一句的驗證失敗,所以我花了一看jsonapi規格):

{ 
    "data": [ 
    { 
     "type": "inventory", 
     "id": "INV0001", 
     "attributes": { 
     "inv_inventory_id": "INV0001", 
     "inv_owner_company_id": 1, 
     "inv_owner_department_id": 1, 
     "inv_user_department_id": 1, 
     "inv_user_worker_id": 1, 
     "title": "Schreibtisch" 
     }, 
     "links": { 
     "self": { 
      "href": "http://127.0.0.1:8000/api/v2/inventory/INV0001" 
     }, 
     "user": { 
      "href": "http://127.0.0.1:8000/api/v2/worker/1" 
     }, 
     "owner_dept": { 
      "href": "http://127.0.0.1:8000/api/v2/department/1" 
     }, 
     "owner_comp": { 
      "href": "http://127.0.0.1:8000/api/v2/company/1" 
     } 
     }, 
     "relationships": { 
     "worker": { 
      "data": { 
      "type": "worker", 
      "id": "1" 
      } 
     }, 
     "department": { 
      "data": { 
      "type": "department", 
      "id": "1" 
      } 
     }, 
     "company": { 
      "data": { 
      "type": "company", 
      "id": "1" 
      } 
     } 
     } 
    } 
    ], 
    "included": [ 
    { 
     "type": "worker", 
     "id": "1", 
     "attributes": { 
     "wrk_forename": "Moritz", 
     "wrk_surname": "ASDF", 
     "wrk_department_id": 2, 
     "wrk_homeoffice": true, 
     "wrk_room_id": 1 
     }, 
     "links": { 
     "self": { 
      "href": "http://127.0.0.1:8000/api/v2/worker/1" 
     }, 
     "hardware": { 
      "href": "http://127.0.0.1:8000/api/v2/worker/1/hardware" 
     }, 
     "software": { 
      "href": "http://127.0.0.1:8000/api/v2/worker/1/software" 
     }, 
     "inventory": { 
      "href": "http://127.0.0.1:8000/api/v2/worker/1/inventory" 
     }, 
     "accessory": { 
      "href": "http://127.0.0.1:8000/api/v2/worker/1/accessory" 
     } 
     } 
    }, 
    { 
     "type": "department", 
     "id": "1", 
     "attributes": { 
     "department": "Entwicklung", 
     "dept_floor_id": 3 
     }, 
     "links": { 
     "self": { 
      "href": "http://127.0.0.1:8000/api/v2/department/1" 
     }, 
     "floor": { 
      "href": "http://127.0.0.1:8000/api/v2/floor/3" 
     }, 
     "hardware": { 
      "href": "http://127.0.0.1:8000/api/v2/department/1/hardware" 
     }, 
     "software": { 
      "href": "http://127.0.0.1:8000/api/v2/department/1/software" 
     }, 
     "inventory": { 
      "href": "http://127.0.0.1:8000/api/v2/department/1/inventory" 
     }, 
     "accessory": { 
      "href": "http://127.0.0.1:8000/api/v2/department/1/accessory" 
     } 
     } 
    }, 
    { 
     "type": "company", 
     "id": "1", 
     "attributes": { 
     "company": "GermanPersonnel", 
     "com_building_id": 1 
     }, 
     "links": { 
     "self": { 
      "href": "http://127.0.0.1:8000/api/v2/company/1" 
     } 
     } 
    } 
    ], 
    "links": { 
    "self": { 
     "url": "http://127.0.0.1:8000/api/v2/inventory?page[number]=1&page[size]=10" 
    }, 
    "first": { 
     "url": "http://127.0.0.1:8000/api/v2/inventory?page[number]=1&page[size]=10" 
    }, 
    "last": { 
     "url": "http://127.0.0.1:8000/api/v2/inventory?page[number]=1&page[size]=10" 
    } 
    }, 
    "meta": { 
    "page": { 
     "total": 1, 
     "last": 1, 
     "number": 1, 
     "size": 10 
    } 
    }, 
    "jsonapi": { 
    "version": "1.0" 
    } 
} 

但根據上jsonapi.org規格的鏈接應該像

"links": { 
    "self": "http://127.0.0.1:8000/api/v2/inventory/INV0001" 
    }, 

我的問題是:

是否合法顯示鏈接,如在我的示例中輸出爲帶有「href」的對象 ?我很困惑,因爲我使用的包是jsonapi.org上列出的 ,但似乎不符合規格。

BTW:我的英語可能是一個有點混亂,但我希望我descriped我的問題以及可能的

+1

我從來沒有見過API使用'href'來表示鏈接。通常它就像'foo_url:「http://www.coolapi.com/api/v3/foo/12」' – jready

+0

因此,以字符串形式顯示鏈接是正確的方法嗎? –

回答

1

這實際上是對規範有效的JSON API輸出基地,

http://jsonapi.org/format/#document-links

如果指定,可以使用鏈接成員來表示鏈接。每個鏈接成員的值必須是一個對象(一個「鏈接對象」)。

有效採樣從規格

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

一個鏈接對象的每個成員是一個「鏈接」。鏈接必須表示爲:

  • 包含鏈接URL的字符串。
  • 的對象(「鏈接對象」)可以包含以下成員:
    • HREF:包含鏈接的URL的字符串。
    • meta:元對象,包含有關鏈接的非標準元信息。

因此,你的輸出實際上是有效的。