14

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. ?

tereško
  • 58,060
  • 25
  • 98
  • 150
Rodion Gorkovenko
  • 2,670
  • 3
  • 24
  • 37

3 Answers3

8

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.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
  • Thanks for explanation. Mentioning of ActiveRecord pattern makes things bit more clear to me. As about your personal opinion - which PHP frameworks you think are better following MVC idea and good practices? – Rodion Gorkovenko Aug 13 '12 at 04:10
  • 3
    @RodionGorkovenko , currently the best one, in regards to coding practice, is considered Symfony2. But it's like being smartest kind in remedial class. And Sf2 is [not an MVC framework](http://fabien.potencier.org/article/49/what-is-symfony2). But you should be able to use MVC design pattern with it. Most of PHP frameworks, that claim to be "MVC frameworks", are following the RubyOnRails interpretation of MVC (view is template and model is ORM), with all the downsides included. – tereško Aug 13 '12 at 04:31
  • Love your answer @tereško. Please keep it up! – Kim Stacks Feb 19 '13 at 03:40
1

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.

Marcel Hebing
  • 3,072
  • 1
  • 19
  • 22
  • Hm... It looks I never spoke of placing business logic in controllers. Though I did not think of placing it in Models too. I thought Components in PhpCake are something like our Services in java EE, but they look more auxiliary... – Rodion Gorkovenko Aug 12 '12 at 19:11
0

In CakePHP. the "M" is just a bunch of Data Models instead of Domain Models. In my opinion. CakePHP is made for RAD development. It is not a good fit for enterprise applications.

My opinion though.

XuDing
  • 1,982
  • 18
  • 27