2017 © Pedro Peláez
 

silverstripe-vendormodule restfulserver

Add a RESTful API to your SilverStripe application

image

silverstripe/restfulserver

Add a RESTful API to your SilverStripe application

  • Thursday, July 26, 2018
  • by chillu
  • Repository
  • 15 Watchers
  • 39 Stars
  • 59,748 Installations
  • PHP
  • 7 Dependents
  • 2 Suggesters
  • 42 Forks
  • 6 Open issues
  • 19 Versions
  • 9 % Grown

The README.md

SilverStripe RestfulServer Module

Build Status Scrutinizer Code Quality codecov SilverStripe supported module, (*1)

Overview

This class gives your application a RESTful API. All you have to do is set the api_access configuration option to true on the appropriate DataObjects. You will need to ensure that all of your data manipulation and security is defined in your model layer (ie, the DataObject classes) and not in your Controllers. This is the recommended design for SilverStripe applications., (*2)

Requirements

  • SilverStripe 4.0 or higher

For a SilverStripe 3.x compatible version of this module, please see the 1.0 branch, or 1.x release line., (*3)

Configuration

Example DataObject with simple API access, giving full access to all object properties and relations, unless explicitly controlled through model permissions., (*4)

namespace Vendor\Project;

use SilverStripe\ORM\DataObject;

class Article extends DataObject {

    private static $db = [
        'Title'=>'Text',
        'Published'=>'Boolean'
    ];

    private static $api_access = true;
}

Example DataObject with advanced API access, limiting viewing and editing to Title attribute only:, (*5)

namespace Vendor\Project;

use SilverStripe\ORM\DataObject;

class Article extends DataObject {

    private static $db = [
        'Title'=>'Text',
        'Published'=>'Boolean'
    ];

    private static $api_access = [
        'view' => ['Title'],
        'edit' => ['Title']
    ];
}

Example DataObject field mapping, allows aliasing fields so that public requests and responses display different field names:, (*6)

namespace Vendor\Project;

use SilverStripe\ORM\DataObject;

class Article extends DataObject {

    private static $db = [
        'Title'=>'Text',
        'Published'=>'Boolean'
    ];

    private static $api_access = [
        'view' => ['Title', 'Content'],
    ];

    private static $api_field_mapping = [
        'customTitle' => 'Title',
    ];
}

Given a dataobject with values:, (*7)

    ID: 12
    Title: Title Value
    Content: Content value

which when requesting with the url /api/v1/Vendor-Project-Article/12?fields=customTitle,Content and Accept: application/json the response will look like:, (*8)

{
    "customTitle": "Title Value",
    "Content": "Content value"
}

Similarly, PUT or POST requests will have fields transformed from the alias name to the DB field name., (*9)

Supported operations

  • GET /api/v1/(ClassName)/(ID) - gets a database record
  • GET /api/v1/(ClassName)/(ID)/(Relation) - get all of the records linked to this database record by the given reatlion
  • GET /api/v1/(ClassName)?(Field)=(Val)&(Field)=(Val) - searches for matching database records
  • POST /api/v1/(ClassName) - create a new database record
  • PUT /api/v1/(ClassName)/(ID) - updates a database record
  • PUT /api/v1/(ClassName)/(ID)/(Relation) - updates a relation, replacing the existing record(s) (NOT IMPLEMENTED YET)
  • POST /api/v1/(ClassName)/(ID)/(Relation) - updates a relation, appending to the existing record(s) (NOT IMPLEMENTED YET), (*10)

  • DELETE /api/v1/(ClassName)/(ID) - deletes a database record (NOT IMPLEMENTED YET), (*11)

  • DELETE /api/v1/(ClassName)/(ID)/(Relation)/(ForeignID) - remove the relationship between two database records, but don't actually delete the foreign object (NOT IMPLEMENTED YET)
  • POST /api/v1/(ClassName)/(ID)/(MethodName) - executes a method on the given object (e.g, publish)

You can trigger searches based on the fields specified on DataObject::searchable_fields and passed through DataObject::getDefaultSearchContext(). Just add a key-value pair with the search-term to the url, e.g. /api/v1/(ClassName)/?Title=mytitle., (*12)

Other url-modifiers

  • &limit=<numeric>: Limit the result set
  • &relationdepth=<numeric>: Displays links to existing has-one and has-many relationships to a certain depth (Default: 1)
  • &fields=<string>: Comma-separated list of fields on the output object (defaults to all database-columns). Handy to limit output for bandwidth and performance reasons.
  • &sort=<myfield>&dir=<asc|desc>
  • &add_fields=<string>: Comma-separated list of additional fields, for example dynamic getters.

Access control

Access control is implemented through the usual Member system with BasicAuth authentication only. By default, you have to bear the ADMIN permission to retrieve or send any data. You should override the following built-in methods to customize permission control on a class- and object-level:, (*13)

  • DataObject::canView()
  • DataObject::canEdit()
  • DataObject::canDelete()
  • DataObject::canCreate()

See SilverStripe\ORM\DataObject documentation for further details., (*14)

You can specify the character-encoding for any input on the HTTP Content-Type. At the moment, only UTF-8 is supported. All output is made in UTF-8 regardless of Accept headers., (*15)

The Versions

26/07 2018

dev-master

9999999-dev

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Hamish Friedlander

api rest silverstripe

11/06 2018

2.1.x-dev

2.1.9999999.9999999-dev

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Hamish Friedlander

api rest silverstripe

11/06 2018

2.1.0

2.1.0.0

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Hamish Friedlander

api rest silverstripe

05/06 2018

2.0.x-dev

2.0.9999999.9999999-dev

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Hamish Friedlander

api rest silverstripe

09/04 2018

2.0.3

2.0.3.0

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Hamish Friedlander

api rest silverstripe

04/04 2018

2.0.2

2.0.2.0

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Hamish Friedlander

api rest silverstripe

22/03 2018

2.0.1

2.0.1.0

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Hamish Friedlander

api rest silverstripe

08/02 2018

2.0.0

2.0.0.0

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Hamish Friedlander

api rest silverstripe

07/12 2017

2.0.0-beta1

2.0.0.0-beta1

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Hamish Friedlander

api rest silverstripe

17/11 2017

1.0.x-dev

1.0.9999999.9999999-dev

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

by Hamish Friedlander

api rest silverstripe

17/11 2017

1.0.8

1.0.8.0

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

by Hamish Friedlander

api rest silverstripe

25/08 2017

1.0.6

1.0.6.0

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

by Hamish Friedlander

api rest silverstripe

25/08 2017

1.0.7

1.0.7.0

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

by Hamish Friedlander

api rest silverstripe

04/02 2016

1.0.5

1.0.5.0

Add a RESTful API to your SilverStripe application

  Sources   Download

BSD-3-Clause

The Requires

 

by Hamish Friedlander

api rest silverstripe

19/11 2015

1.0.4

1.0.4.0

Add a RESTful API to your SilverStripe application

  Sources   Download

The Requires

 

by Hamish Friedlander

api rest silverstripe

22/07 2015

1.0.3

1.0.3.0

Add a RESTful API to your SilverStripe application

  Sources   Download

The Requires

 

by Hamish Friedlander

api rest silverstripe

14/07 2013

1.0.1

1.0.1.0

Add a RESTful API to your SilverStripe application

  Sources   Download

The Requires

 

by Hamish Friedlander

api rest silverstripe

14/07 2013

1.0.1-rc1

1.0.1.0-RC1

Add a RESTful API to your SilverStripe application

  Sources   Download

The Requires

 

by Hamish Friedlander

api rest silverstripe

14/07 2013

1.0.2

1.0.2.0

Add a RESTful API to your SilverStripe application

  Sources   Download

The Requires

 

by Hamish Friedlander

api rest silverstripe