S
S
Sergey Khlopov2020-05-17 10:46:58
Software design
Sergey Khlopov, 2020-05-17 10:46:58

How to organize custom classes?

In order not to clog the controller with numerous lines of code, I would like to put the logic into some separate classes, and simply call the methods of these classes in the controller. So that the controller is only engaged in receiving data and sending. And what is the best way to do this? I found this article here - https://laravel.demiart.ru/refactoring-services-st... here we create the app/Services folder and put the classes there, is it right to do this? Or is there some other way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sarvarov, 2020-05-17
@Shlop

Yes, all logic needs to be taken out in separate classes.
The task of the controller is to determine what information to display based on the received request. Even validation is better to take out in a separate Request, because this is no longer the task of the controller.
I usually do services like you wrote, but also:
/app/Repositories - repositories. They allow to take information from the database in the controller, such as this:
$this->postsRepository->getPostsForHomePageLoop();
In general, there are many ways - you can read about design patterns.
The main thing to remember is SRP from SOLID. That each class is responsible for its functionality and cannot go beyond its scope.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question