Answer the question
In order to leave comments, you need to log in
How to properly organize application logic in Laravel?
Hello!
I’m immersing myself in the world of Laravel a little, and I have a question: how to organize the application logic?
A little more detail:
there is an ArticleController controller,
there is a Models\Article Model
and a repository Repositories\ArticleRepository
In addition, there are categories of articles for which a separate model, a separate repository.
We take the show controller's show method, to view a specific article
in it, we access the repository and get our article or 404 we pass to the view I
want to add some logic and data transformation:
1) convert the date to the desired format
2) increase the number of views
3) get a separate number of
likes and dislikes (they are stored in the same table
)
dislikes.
The question is how then to transfer this data to the view, as a separate array or modify the model.
Probably, with an array of modified data, this is complete nonsense, because if it is not viewing an article, but displaying a list, then you will get porridge.
In general, I would like to hear / see how someone does on real projects
Answer the question
In order to leave comments, you need to log in
we get our article or 404 and pass it to view
Yes, yes, 99% of hamsters make a Repository without even understanding why, and only a database and eloquent as a data source ...
Yes, it’s right to make a Service for logic. and DTO to pass complex data to Service.
You pass whatever you want to the view, because the data for the view is prepared by the controller as you want.
On real projects, horses are driven and 99% of the logic, hell knows where and how, everything is transmitted through the stump of the deck :)
Business does not like to pay for beautiful architecture because it takes 3 times longer and rarely shoots in the future.
Yes, there are places where it will save time, but often it is an overhead.
But it is better to introduce a minimum Service layer.
Another thing is that if you enter it incorrectly !!! and often it happens if you are not a signor or an architect, and then these crooked services interfere, not help. But the attempt is not torture, especially if the customer is ready to pay you 5 times more than if they wrote on models ...
Yes, yes, everything is bad.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question