This question is on my mind for some time now.
Let's say I have a PHP website which has an API (SOAP, REST, etc.) and this API serves almost the same things that are available through the pages (e.g. blog post, comments, statistics, etc.).
In order to avoid code redundancy/duplication I'm thinking of using the API even for the site itself because, although most of code factorization is already done by using MVC's Model, some logic is still in double if a site has to offer an API.
Example when a visitor retrieves a given blog's post:
Classic GUI:
- HTTP request: sent by browser
- PHP Controller: read user input
- PHP Model: fetch the post from database
- PHP Controller: populate the view
- PHP View: render HTML
- HTTP response: sent to browser
GUI using API:
- HTTP request: sent by browser
- PHP "posts" Controller: read user input
- HTTP API request: sent by PHP Controller to site's API URL
- PHP "api" Controller: read API request
- PHP Model: fetch the post from database
- PHP "api" Controller: populate the "api" view
- PHP "api" View: render (XML, JSON, etc.)
- HTTP API response: sent to PHP Controller of point 2.
- PHP Controller: populate the view
- PHP View: render HTML
- HTTP response: sent to browser
My opinion is that GUI should be a layer that consumes API (a new layer) but I am concerned about performances issues because, in PHP websites, an API call goes through an HTTP request which takes time.