Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP - where to put service logic

I am Java programmer who tries to investigate CakePHP - currently I have problem with application structure/design. I could not understand where to put core logic of application.

When I am developing in JavaEE, common approach looks like following:

  • Model classes are simple beans which represent data entities (products, people etc) - mostly like data structures with getters/setters;

  • Controller classes are simple enough classes which aggregate necessary data and inject them into dedicated View template which is then sent to user;

  • DAO (DataAccessObject) or Repository classes are ones which can load and store entities into database;

  • Service classes are usually singletons which contains certain business-logic methods - these are called by controllers, by other services or by scheduled actions, on the other hand they themselves call DAO / Repository methods to fetch or modify data.

For example if I have entities Person, Product and Order, when user selects some product and clicks "put it into my cart/basket" new Order for this Person should be created and this Product should be added to this Order (we may check that Person is not bad debtor and that Product is present at store etc.) - all this work is performed in methods of OrderService called by some controller.

Usually some kind of IOC (Inversion of Control) is used so that all services and controllers have links to necessary services etc.

Now I am slightly bewildered about how this all is done in CakePHP. Where should I put this business-logic etc. ?

like image 294
Rodion Gorkovenko Avatar asked Aug 12 '12 17:08

Rodion Gorkovenko


2 Answers

In CakePHP the model layer is made up from collection of active record instances, called AppModel. They combine the storage-related logic (that you would usually put in DAOs and/or Repositories) with business logic (what usually went into your "models").

Any other domain related logic (from your Service) becomes part of controller.

If you want to know, how you are supposed to implement domain business logic in CakePHP, just look up articles which praise active record pattern.

Personal opinion
CakePHP and CodeIgniter are two of the worst frameworks in PHP.
They are filled with bad practices.

Actually, if you were doing correct-ish MVC, then model layer would contain all of the business logic and everything that is related to it. Model layer is made of DAOs, Repositories, Domain Objects (what you call "models") and Services.

While your descriptions of Java-based code indicates, that you are kinda moving in that direction, CakePHP is not even remotely close to it.

Then again, it might be that my understanding of MVC is just wrong.

like image 113
tereško Avatar answered Nov 09 '22 21:11

tereško


The controllers should only contain logic relevant for the whole thing being a web application. Your business logic belongs into the models. I think it is one of the basic mistakes that you find in many cakePHP applications, that to much logic is put into the controllers, which actually belongs into the models.

like image 1
Marcel Hebing Avatar answered Nov 09 '22 21:11

Marcel Hebing