PHP – Can REST API Be Used as Business Layer?

codeigniterdesign-patternsmvcPHPrest

I am using PHP Codeigniter MVC design pattern

and I had this project with some sort of specific business processes

In my application I will be dealing with 2 existing REST APIs:

  1. Google
  2. Trello

I came up with idea to create REST API to act as Business Logic Layer (BBL)

that in turns access my models directly to fetch needed data to formulate business rules

and controller with communicate with BLL with REST client,
enter image description here

Is that bad approach for performance ?

Is it better to create 2 layers of Models one as Data Access Layer (DAL) and one as Business Logic Layer (BLL)

Best Answer

The problem I see with your approach is that you are building a REST API for only one consumer, your controllers, and that's overkill. Don't just add a layer just to pass data from one layer to another, there's no point, you'll be creating work for yourself for no additional benefit.

One (quick) way to make your API useful would be if it was the only endpoint your controllers (i.e. your application) ever needs. Consider this:

enter image description here

Suddenly your REST API has a purpose, and that is to provide a unified interface to your various data providers. Your application doesn't need to know about the Google or the Trello API, or any other data provider you might use in the future, it just needs to know about your REST API.

This design, albeit slightly more useful, is still overkill if your application is the only consumer. The whole point of building a REST API is to expose a publicly available interface for your applications to share. And the key here is exposure, your REST API will be available to the world and therefore it must be secure. API authentication and authorization are no simple tasks, if only one application uses your API, why go through all the trouble? Unless of course you want to experiment and learn. If that's the case, by all means go for it.

In any case, performance shouldn't be an issue. You'd be adding an extra layer, so obviously there will be some overhead, but whether that overhead will be significant or not depends entirely on your implementation. If your REST API is the single endpoint, then it will probably be a good idea to also be the one place where caching happens. And not just server side caching, a REST API makes browser caching a bit more easier to exploit, and if you get it right, the overall performance of your application may increase, comparing to a non REST approach.

tl;dr: Don't build stuff that don't have an actual purpose (unless you're learning).