Rest Controller

Direct access to your documents via a json rest api

Expose documents via json rest.

Enable Shard Rest

The rest extenions needs to be configured with an endpoint map. The endpoint map defines which documents will be exposed by the rest controller, and what urls will be used to access them. Eg:

  1. 'zoop' => [
  2. 'shard' => [
  3. 'manifest' => [
  4. 'default' => [
  5. 'extension_configs' => [
  6. 'extension.rest' => [
  7. 'endpoint_map' => [
  8. 'users' => [
  9. 'class' => 'My\Users\Document',
  10. 'property' => 'username'
  11. ]
  12. ]
  13. ]
  14. ]
  15. ]
  16. ]
  17. ]
  18. ]

Embedded Lists

To make embedded lists accessable via rest, configure them like this:

  1. ...
  2. 'endpoint_map' => [
  3. 'users' => [
  4. 'class' => 'My\Users\Document',
  5. 'property' => 'username',
  6. 'embedded_lists' => [
  7. 'assets' => [
  8. 'property' => 'name',
  9. 'class' => 'My\Assets\Document'
  10. ]
  11. ]
  12. ]
  13. ]
  14. ...

Customize Route

Shard Module automatically configures the rest controller on the /rest route. You may wish to override that in your module config.

By default, the config above would expose the user with username toby at:

  1. http://myserver.com/rest/users/toby

Access Control

If the Access Control extension is enabled, all permissions will be respected.

Get a single document.

To get a document use:

  1. http://myserver.com/rest/user/toby

Partial documents

To get only some properties of a document use:

  1. http://myserver.com/rest/user/toby?select(username,age)

Single nested document

Append the property name:

  1. http://myserver.com/rest/user/toby/address

Single document from a nested list

To get one item from a list of documents append the property name and the id of the document you want:

  1. http://myserver.com/rest/user/toby/assets/funky-car

Errors

error description
404 If the endpoint requested does not exists, or the document requested cannot be found.

Get a list of documents.

An array of documents will be returned when a list is requested, rather than a specific document. A successful response will include a contentRange: x-x/x header.

  1. http://myserver.com/rest/user

Partial documents

  1. http://myserver.com/rest/user?select(username,age)

Filter

  1. http://myserver.com/rest/user?country=australia

And Filter

  1. http://myserver.com/rest/user?country=australia&type=active

Or Filter

  1. http://myserver.com/rest/user?country=[australia, usa]

Sort Ascending

  1. http://myserver.com/rest/user?sort(+username)

Sort Decending

  1. http://myserver.com/rest/user?sort(-username)

Multiple Sort

  1. http://myserver.com/rest/user?sort(+country,+username)

Offset

  1. HEADERS
  2. range: items=2-25
  3. http://myserver.com/rest/user

Nested List

  1. http://myserver.com/rest/assets

Errors

error description
404 If the endpoint requested does not exist.
416 Content-Type: application/api-problem+json, Requested range cannot be returned. Occurs if a bad range is requested, eg: Range: 10-5
204 If the requested list is empty.

Create a document

To create a new document, use a POST request, and place the json document in the request body.

  1. POST
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"username": "lucy", "age": 27}
  7. http://myserver.com/rest/user

Single nested document

  1. POST
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"street": "Street Rd", "number": "45", city: "Sydney"}
  7. http://myserver.com/rest/user/address

Single document from a nested list

  1. POST
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"make": "ford", "model": "stumpy"}
  7. http://myserver.com/rest/user/assets/old-car

Create with reference

Add a reference to an already existing document.

  1. POST
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"username": "jsason", "country": {"$ref": "country/australia"}}
  7. http://myserver.com/rest/user

Errors

error description
404 If the endpoint or document requested does not exist.
500 Content-Type: application/api-problem+json, Document validation failed. Occurs if the validation extension is turned on, and validation fails.
500 Content-Type: application/api-problem+json, Document already exists. Occurs request to create a document with id that already exists.

Update a document.

Note: PUT will update all properties of a document. If you need to update only some of the properties of a doument, use PATCH.

The response from a successful PUT will always be a 204.

Update a document

  1. PUT
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"age": "18"}
  7. http://myserver.com/rest/user/toby

Create via PUT

If the document selected for update does not exist, it will be created.

  1. PUT
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"age": "27"}
  7. http://myserver.com/rest/user/lucy

Update single nested document

Append the property name:

  1. PUT
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"street": "Street Rd", "number": "45", city: "Sydney"}
  7. http://myserver.com/rest/user/toby/address

Update single document in a nested list

To update one item from a list of documents append the property name and the id of the document you want:

  1. PUT
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"make": "gm", "model": "camero"}
  7. http://myserver.com/rest/user/toby/assets/funky-car

Replace a whole list

  1. PUT
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. [
  7. {"username": "gumpy"},
  8. {"username": "sleepy"}
  9. {"username": "happy"}
  10. ]
  11. http://myserver.com/rest/user

Update document id

A document id can be updated. To do so, include the new document id in the PUT data. Note that this will actually delete the existing document, and create a new document with the new id.

  1. PUT
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"username": "toby-different"}
  7. http://myserver.com/rest/user/toby

Errors

error description
404 If the endpoint or document requested does not exist.
500 Content-Type: application/api-problem+json, Document validation failed. Occurs if the validation extension is turned on, and validation fails.

Patch a document.

Note: PATCH will update a selection of properties on a document.

The response from a successful PATCH will always be a 204.

Patch a document

  1. PATCH
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"age": "18"}
  7. http://myserver.com/rest/user/toby

Create via PATCH

If the document selected for patch does not exist, it will be created.

  1. PATCH
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"age": "27"}
  7. http://myserver.com/rest/user/lucy

Patch single nested document

Append the property name:

  1. PATCH
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"street": "Street Rd"}
  7. http://myserver.com/rest/user/toby/address

Patch single document in a nested list

To patch one item from a list of documents append the property name and the id of the document you want:

  1. PATCH
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"make": "gm", "model": "camero"}
  7. http://myserver.com/rest/user/toby/assets/funky-car

Patch whole list

Patching a list will add the supplied items to the list, without removing the existing items.

  1. PATCH
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. [
  7. {"username": "gumpy"},
  8. {"username": "sleepy"}
  9. {"username": "happy"}
  10. ]
  11. http://myserver.com/rest/user

Patch document id

A document id can be patched. To do so, include the new document id in the PATCH data. Note that this will actually delete the existing document, and create a new document with the new id.

  1. PATCH
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {"username": "toby-different"}
  7. http://myserver.com/rest/user/toby

Errors

error description
404 If the endpoint or document requested does not exist.
500 Content-Type: application/api-problem+json, Document validation failed. Occurs if the validation extension is turned on, and validation fails.

Delete a document.

The response from a successful DELETE will always be a 204.

Delete a document

  1. DELETE
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. http://myserver.com/rest/user/toby

Delete a list

  1. DELETE
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. http://myserver.com/rest/user

Delete single nested document

Append the property name:

  1. DELETE
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. http://myserver.com/rest/user/toby/address

Delete single document in a nested list

To delete one item from a list of documents append the property name and the id of the document you want:

  1. DELETE
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. http://myserver.com/rest/user/toby/assets/funky-car

Errors

error description
404 If the endpoint or document requested does not exist.

Execute multiple requests at once

A batch request is always a POST to the batch endpoint. The POST content describes the requests which should be executed in order. The response content will describe the result of each request.

For example, this batch request will be the same as executing the nine separate requests in order:

  1. POST
  2. HEADERS
  3. Content-type: application/json
  4.  
  5. CONTENT
  6. {
  7. "get single author": {
  8. "uri": "/rest/author/james",
  9. "method": "GET"
  10. },
  11. "get author list": {
  12. "uri": "/rest/author",
  13. "method": "GET"
  14. },
  15. "get author list of partials": {
  16. "uri": "/rest/author?select(name)",
  17. "method": "GET"
  18. },
  19. "get filtered author list": {
  20. "uri": "/rest/author?country=germany",
  21. "method": "GET"
  22. },
  23. "get author list offset": {
  24. "uri": "/rest/author",
  25. "method": "GET",
  26. "headers": {
  27. "Range": "items=2-100"
  28. }
  29. },
  30. "replace game list": {
  31. "uri": "/rest/game",
  32. "method": "POST",
  33. "content": {"name": "forbidden-island", "type": "co-op"}
  34. },
  35. "delete an author": {
  36. "uri": "/rest/author/harry",
  37. "method": "DELETE"
  38. },
  39. "update a game": {
  40. "uri": "/rest/game/feed-the-kitty",
  41. "method": "PUT",
  42. "content": {"type": "childrens", "author": {"$ref": "author/harry"}}
  43. },
  44. "patch a game": {
  45. "uri": "/rest/game/feed-the-kitty",
  46. "method": "PATCH",
  47. "content": {"type": "kids"}
  48. }
  49. }
  50.  
  51. http://myserver.com/rest/batch

The response object might look like:

  1. {
  2. "get single author": {
  3. "status": 200,
  4. "content": { ... }
  5. },
  6. "get author list": {
  7. "status": 200,
  8. "headers": {
  9. "Content-Range": "x-x/x"
  10. },
  11. "content": [ ... ]
  12. },
  13. "get author list of partials": {
  14. "status": 200,
  15. "headers": {
  16. "Content-Range": "x-x/x"
  17. },
  18. "content": [ ... ]
  19. },
  20. "get filtered author list": {
  21. "status": 200,
  22. "headers": {
  23. "Content-Range": "x-x/x"
  24. },
  25. "content": [ ... ]
  26. },
  27. "get author list offset": {
  28. "status": 200,
  29. "headers": {
  30. "Content-Range": "x-x/x"
  31. },
  32. "content": [ ... ]
  33. },
  34. "replace game list": {
  35. "status": 204
  36. },
  37. "delete an author": {
  38. "status": 204
  39. },
  40. "update a game": {
  41. "status": 204
  42. },
  43. "patch a game": {
  44. "status": 204
  45. }
  46. }