Search code examples
phplaravelarchitecturebusiness-logic

Where I must contain business logic of my application?


After read a lot of articles about right architecture application, I still have a question: where app business logic must contains? Becouse someone told, that logic must contains in models (skinny controllers), another said that models must contain only Database operations logic.

For example:

In my project (online shop) I have a products filter, that used in CategoryController and filtered by Products and Parameters table. So it's not a controller and not a model. I solved it by creation of new directory named Filters (yes, there are few different filters), and contain all logic there. But i dont know is it right solution? I think not, but I dont know how to build it correctly.

So here is my questions:

  1. Did i do the right thing?
  2. Where I must contain business logic?

Thanks and have a nice day!

P.s Sorry for my english.


Solution

  • It's down to preferences. As long as you are using the correct standards, you should be fine!

    So in Laravel, I use Models strictly for database operations. I also create a folder called Services and another folder called Hydrators

    My services in the service folder handles the business logic e.g grabbing data from the models and any logical operations. My hydrators take the data and sort it in the way I want the data to be presented to the view.

    Both my services and hydrators take on the Single Responsibility Principle as this allows me to reuse the same code elsewhere to avoid code duplication!

    My controllers are just an entry point to the back end and only do two things; call the services and stitch the ones needed together and return a result (a result could be anything from JSON to a view with data).

    This is just my personal way of doing things. Others may have a different way.