1

In typical REST/API application, build on MVC architectural pattern, what is the best available technique for acquiring data for rendering views?

Should it be:

  • as in any non-API application: data is acquired directly from DB (through models), as in classical MVC, and controllers are rendering views basing on standard model data, and forms sends data through typical POST,

  • since this is an API application, even its very own website should be a client, that acquires data through API requests only and controllers are rendering views basing on JSON-like data, and forms must encode data to a typical JSON and push it API-way, just like an average JSON PUT request,

  • something else.

This question may sound silly or even naive. Unfortunately, I'm a very beginner to API/REST topic. Sorry.

2 Answers 2

2

If the data will be consumed by something else, either a frontend UI or another application somewhere else on the Internet, then you need a REST API.

Otherwise, you don't.

2
  • I HAVE a REST API. My mobile apps are using it. Now, I'm building a website (name it frontend, if you wish). I'm asking, if frontend (written in the same language, PHP, and run on the same server as API) should access data directly (models / PDO / MySQL) or if it also should be a client, that is accessing data only through API calls?
    – trejder
    Jun 11, 2015 at 13:52
  • 3
    If the website will always run on a server that resides on the same local intranet as the database, then no, you don't need to access the data through the web services. The whole point of web services is to have machines at different physical locations talk to each other over the Internet. Jun 11, 2015 at 13:56
2

There are two methods for running a standard site (non-RESTful) in parallel with a RESTful service.

  1. The website consumes the RESTful service. This would often mean you are creating a one page app (Angular, etc). This is simplest from the backend standpoint in that you should have minimal additional coding on the backend, but you need to create an entire web app in Angular (or other single page app framework)
  2. The website functions as a normal app and you abstract the common code out of the controllers so that all business logic for both services resides in one place. For instance, you have a create user action, the generation of the form HTML and redirecting after form submission is handled in the non-RESTful controller and the formatting of the JSON data is handled in the RESTful controller. The actual logic of saving the user would reside either in a repository or in an abstract controller that both of the other two controllers extend off of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.