Answer the question
In order to leave comments, you need to log in
Where is the best place to put the same logic for different controllers/models?
I am writing an application in laravel and I have a question. Where is the best place to put the same logic for different parts of the application? To make a "parent" model / controller and inherit it in the right place, I think it's stupid. Where is the best place to take these things?
For example, in the helper, I put out small functions like isJson, and so on. Which can be required in any part of the application and should not perform any special functions.
But what about massive things? For example, filtering two complex elements overlapping each other. I don't think it's good to dump all this into a helper.
And in general, where do you prefer to take out "heavy" logic in the application? Why not make fat controllers and not litter the models?
Answer the question
In order to leave comments, you need to log in
Always try to look at the "older" framework where the whole thing is ported from the first day of laravel's creation. (in a good way without holivar).
For a long time in rails, I just put the general logic into modules, I don’t know how it’s done specifically in php now, but conceptually it’s just a set of functional methods (stateless) combined in a namespace - in fact, it can be any class.
In general, an inclusion is made and used.
Speaking about rails, as a result, concerns appeared there for this function - what will be in laravel for this is unknown, but you can just safely use functional classes. (functional programming)
UPD actually in the rails there are concerns for this, helpers for another - but in your case, helpers will help if you do not make "your" concerns as I indicated.
At you, to a campaign, it is generally business logic. You need a service layer.
Why is not it good? What exactly is "bad" about creating a helper that implements a particular service and exposes that service to various parts of the application?
Making a fat helper and littering it with functions that are not related to each other is really not good. But no one is forcing you to have a single helper for the entire application.
In helpers. They are designed for this
> do not clutter up the model?
Model is the most unfortunate word that exists in the world of web development.
The helper is a full part of the model. And what you call a model - a layer of work with a database - is either DBAL or ORM, a date mapper.
In order not to litter the mapper, you need a . The same helper, but working with the database, a collection of specific SQL queries.
Yes, anywhere. What you described (isJson, etc.) - IMHO they have a place just in helpers. And so - everything depends on the functionality. Services, repositories, aggregates, widgets, behaviors, traits, filters, factories, builders, adapters, etc. - huge list
Remember that you are not writing in a framework, but in PHP. Make classes and write all the necessary functionality there. Fortunately, in Laravel it allows you to include classes in the arguments of the constructor of the controller, command, etc. Name the classes so that you can understand later. Place them in the Services folder, for example, or in folders named after the functionality of the application - Orders, Invoices, etc. - as you prefer.
Write classes according to the SOLID principle, so that they are highly specialized, loosely related to each other, etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question