2

I am creating app based on PHP framework Laravel and MySQL. I have Site model with no more than 100 records in it.

For every Site I must make some calculations and get array of values as result. The problem is, due to the specifics of the sites, each single site requires different calculations with different input values. This leads to completely different code for different Sites.

My first taught was to create function for every Site in SiteController and call it when needed, but this looks ugly and will make the controller way to fat. Please suggest better method to deal with this.

tereško
  • 58,060
  • 25
  • 98
  • 150
Todor
  • 43
  • 1
  • 8
  • I think this should be considered in the design phase of the system and not in the coding phase, or am I wrong? – Wreigh Jan 24 '18 at 01:30
  • It must be considered in the design phase, but this feature was requested after the system was running. – Todor Jan 24 '18 at 11:35

1 Answers1

0

Put the business logic in a Service. The reason is that you end up respecting principles like Single Responsibility an Separation of Concerns. Your Service layer would handle Business Logic and your Model would handle Database Operations.

Message flow:

Request -> Controller (action) -> Service > Model(s) ->
(back to) -> Service -> Controller -> View -> Response

More details

odan
  • 4,757
  • 5
  • 20
  • 49
  • I agree, but even in Service, this still means dozens of methods in one class. – Todor Jan 24 '18 at 11:32
  • No, because the Service is a layer and not a single class. Within the service layer you can create many small and specialized classes for validation, mapping, calculation etc... – odan Jan 24 '18 at 11:42