0

I have been glancing at a RESTful specification available on http://restfulobjects.org (direct link to spec here). It is a little different from other things I have seen, but it makes sense, and having examples and clear specifications makes my development a lot simpler.

According to the standard, the URIs will be like:

  • api.example.com/object/user/31
  • api.example.com/object/user/31/properties/username
  • api.example.com/object/user/31/collections/channels
  • api.example.com/object/user/31/actions/someFunction
  • api.example.com/object/user/31/actions/someFunction/invoke

See this answer for a brief explanation of the structure. It is quite clear.

Note: This is not a discussion whether or not services/actions can be represented as resources. There is a good discussion on that topic here: http://www.infoq.com/articles/Intro_Restful_Objects. Look towards the end of the comments section.

My project is still on the drawing board, and most design elements up for grasp. I will however be using Symfony. I have my entities, and I have some services that act on those entities, and I have controllers to define what the services will do in a specific context.

And so to my actual questions:

...the Restful Objects spec is not based on the MVC pattern - it makes no assumptions about the architecture of the server that implements the API. In the URL:

~/objects/customers/31/actions/lastOrder/invoke

the 'actions/lastOrder' is not specifying a controller action, it is specifying an action (i.e. a method) on a domain object (customers/31)

  1. How do I structure my controllers in the most efficient way if I want to use the specification in an MVC application? Considering that the controllers would somehow serve different resources (objects, services, domain-types) as well as different representations of that resource. What are my layers? Update Do I have e.g. a customer controller? An generic object controller? A useraction controller?

  2. Do I need to rethink my whole entity/services approach? i.e. the solution for how to implement this is beyond the controller structure? (I fear it is, and then I do not know my next step)

  3. For simplicity/efficiency I would like to make use of the FOSRestBundle and NelmioAPiDocBundle. I believe this can be done, but I then am concerned the spec will only serve as a url template for me, and not being implemented in my logic/design since I have to massage my framework to fit the URL somehow.

Please excuse my ignorance if this question has an obvious answer. I am new to RESTful APIs. If it comes down to a subjective perspective on implementation model, please give me some pointers, because I am currently at a loss for how to approach this.

Community
  • 1
  • 1
bblue
  • 545
  • 5
  • 24

2 Answers2

1

From the "What is Symfony2 ?" article of Fabien Potencier:

Is Symfony2 an MVC framework?

If you look around, every single framework seems to implement the MVC pattern. And most of them are advertised as MVC frameworks... but not Symfony2. Have a look at the documentation, and you will see that the MVC pattern is only mentioned once or twice, but Symfony2 is never defined as being an MVC framework.

A bit later in this post:

Symfony2 is an HTTP framework; it is a Request/Response framework.
That's the big deal. The fundamental principles of Symfony2 are centered around the HTTP specification.
...
Sometimes, you just need a way to create a REST API. Sometimes, the logic is mostly in the browser and the server is just used to serve data (think backbone.js for instance). And for these projects, you don't need an MVC framework. You need something that handles a Request and returns a Response. You need a framework that implements the HTTP specification. HTTP streaming is yet another example that does not fit well with the MVC pattern.
...
And if you like to call Symfony2 an MVC framework, then you should know that Symfony2 is really about providing the tools for the Controller part, the View part, but not the Model part. It's up to you to create your model by hand or use any other tool, like an ORM.

As Symfony is a set of components, you can easily adapt your usage of the framework to the project you are working on, and the principles that you want to follow (here a REST specification).

The FOSRestBundle is very extensible.
You can define your routes manually, create different views of your objects through serialization.

Plus, I think you can keep very light controllers by writing the most of your objects management's logic inside Repository classes.

So,

  • Use simplified models and serialization to have different views/layers of your objects.

  • You should not rethink your whole Entity/Services in order to be more REST, just give them some changes/improvements in order to use them through data-transfer objects/domain objects (models) and Repository classes (fetching/storing methods).

  • You should be able to take benefits of use REST utilities such as FOSRestBundle, it provides more than built-in actions, allows you to make all custom stuff that you could need, be MVC or not, and be REST or not. It's at your own appreciation.

Community
  • 1
  • 1
chalasr
  • 12,971
  • 4
  • 40
  • 82
-1

Have you considered using either Naked Objects (for the .NET platform) or Apache Isis (for the Java platform)? Both of these give you a complete Restful Objects compliant restful API for free - derived entirely from your entity models. (I am responsible for the first of these two. Our implementation is built on top of the ASP.NET WebApi framework. We have a RestfulObjectsController, with many methods on it, but these are all generic methods, corresponding to the various Restful Objects resource types - we do not need to add any new methods specific to the domain objects or their actions.

Richard Pawson
  • 1,494
  • 1
  • 10
  • 15
  • That would be tempting, but unfortunately I only have php experience and I am not about to embark on a completely new language. Would you be able to provide a bit more details for how you have laid out the controller mapping/routing to your entities? I might mirror something like that in php then. – bblue Mar 21 '16 at 19:51