1
votes

I am a little confused on how to go about using the RESTful server API in SilverStripe 3. I have just starting learning and the following has confused me.

All content on our site is stored in a database. Each class that is a child of the DataObject class will have its own table in our database.

Every object of such a class will correspond to a row in that table - this is our "data object", the "model" of Model-View-Controller. A page type has a data object that represents all the data for our page. Rather than inheriting directly from DataObject, it inherits from SiteTree. We generally create a "Page" data object, and subclass this for all other page types. This allows us to define behavior that is consistent across all pages in our site.

I have done this to set up pages but now I am learning about the RESTful server API and it says to create an object that extends DataObject.

Forgive my ignorance but would extending SiteTree not be the same thing?

Very confused so would appreciate some enlightenment.

2
SiteTree too is extending DataObject, but brings a lot of overhead in case you're not working with 'pages' (such as parent/children relationships etc) - schellmax
Thanks for the response schellmax. My confusion lies with structuring the pages. Basically I have two other page types set up which extend Page which extends SiteTree. If I'm understanding correctly extending DataObject in the new one is basically the same thing? - MillyMonster
it's "the same thing" as far as the database generation is concerned, yes - but i'm not sure yet what you're after...? - schellmax
If you have new pages that extend the Page class, then they already extend the DataObject class. SiteTree, Page, and anything that extends those all come from DataObject. Add static $api_access = true; to you custom page classes, and see the docs for more complete instructions on setting up an API interface for your data/pages. - Benjamin Smith
@schellmax Basically what I am trying to do is create a CMS backend that takes in information and outputs it in JSON to a hybrid app. I don't want the information outputted to the published site like it usually is though. - MillyMonster

2 Answers

5
votes

As commented above, if you extend SiteTree, then you are extending DataObject, but getting a lot of overhead. Some of this overhead may be useful to you, as SiteTree provides Versioning, Hierarchies and other nice tricks which make it a good class for Pages in your site.

However, if you are wanting to manage a lot of objects, or if you aren't using the Hierarchy or Versioning models, or if you just want to customize how the objects are presented or managed in the CMS, then it is better to extend DataObject directly. There are various tutorials on this on the SilverStripe documentation site, and on SSBits

This may also be useful to you.

Once you have your DataObject subclass working well for you, you can start to add the RestfulServer capabilities to it. The most basic way to do this is to add a static property to your class:

static $api_access = true; 

More information about using RestfulServer is available here

0
votes

Have you had a look at the following URL : http://doc.silverstripe.org/framework/en/reference/restfulservice

This example allows you to make data available in the RSS format. I'm not sure if there is an easy way to output the data as JSON, but there are plenty of examples on the web of converting rss to json, so you could simply update your app to work with this format and convert if need be.