Using the site API even for the site itself?

This question is in my opinion for some time.

Let's say I have a PHP website that has an API (SOAP, REST, etc.), and this API supports almost the same things that are accessible through the pages (e.g. blog post, comments, statistics and etc.).

To avoid code redundancy / duplication, I think to use the API even for the site itself, because although most of the code factorization is already done using the MVC model, some logic is still double if the site should offer an API.

Example when a visitor receives a blog post:

Classic GUI:

  • HTTP request: sent by browser
  • PHP controller: reading user input
  • PHP model: fetching from a database
  • PHP controller: populate view
  • PHP view: HTML rendering
  • HTTP response: sent to browser

GUI using API:

  • HTTP request: sent by browser
  • PHP "posts" Controller: read user input
  • HTTP API request: sent by the PHP controller to the URL of the site API
  • PHP "api" Controller: read the API request
  • PHP model: fetching from a database
  • PHP "api" Controller: populates the "api" view
  • PHP "api" View: rendering (XML, JSON, etc.)
  • HTTP API response: sent to point 2 PHP controller.
  • PHP controller: populate view
  • PHP view: HTML rendering
  • HTTP response: sent to browser

My opinion is that the GUI should be a layer that consumes the API (a new level), but I'm worried about performance issues because on PHP sites the API call goes through an HTTP request that takes time.

+3
1

, , "" . , HTML Javascript, :

  • HTTP- GUI:
  • HTTP-: HTML ( )

  • API HTTP: URL- API AJAX

  • PHP "api" : API
  • PHP-:
  • PHP "api" : "api" JSON
  • HTTP API: 3.

  • Javascript: DOM

"" , RESTful ( ) , . . .

+1

All Articles