A
A
Artem Kislenko2016-08-12 04:06:34
Laravel
Artem Kislenko, 2016-08-12 04:06:34

How to properly organize code in a php framework?

Hello, newbie question.
How to properly organize the code so that controllers and models do not grow into huge sheets.
Now I'm working on a project written in Laravel, there are a lot of things, but the main problem is that there are long controllers that are responsible for a lot of things. How is it customary to do in php in general - that in the controller we make queries to the database using the ORM or the query builder, aggregate the data and spit it out, or everything needs to be taken out in the model, but then the models will be just huge, what to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2016-08-12
@webwork

Study, study and study again, as grandfather Lenin bequeathed.
Without seeing the code itself and the structure of the project, it is difficult to advise anything here. Read "Clean Code" by Bob Martin, especially about the Single Responsibility Principle. If your class method does not fit on a computer monitor, it must be rewritten. Well and all in such spirit. The main thing is not to chase the purity of the code for the sake of purity itself. All Uncle Bob encourages is to write readable, maintainable code that other developers can read and maintain after you.
And another important point, fix the development rules at the team level. Those. so that everyone writes the same way. The structure of the project should be clear and understandable. We have outlined such rules in our team (they are, of course, being revised and discussed) and now a team of 6 developers easily navigates the code of an enterprise monster with a 6-year history, and with over 5000 classes on board, and even manages to improve it.
Regarding controllers: I am personally not familiar with the Laravel philosophy, I myself develop in Java and my framework is Spring. My philosophy about controllers is this - the controller is an intermediary between the front and back ends. The controller does not contain any logic, from the word "absolutely". All logic is placed in a facade, which in turn can include a bunch of services. Services (if possible) are designed with single responsibility in mind. Services contain all the business logic of the application.

O
OnYourLips, 2016-08-12
@OnYourLips

Делайте сервисную прослойку между контроллером и ActiveRecord классами.
Помните, что это увеличит время и стоимость разработки на 10-20%.
Для небольших проектов проще писать в стиле RoR, работая с AR прямо из контроллеров. Если проект будет расти - отрефакторите.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question