Answer the question
In order to leave comments, you need to log in
How to structure a Laravel application?
I am learning Laravel 6 to write my application.
In this regard, I have a few questions regarding the structuring of the project:
1) The documentation provides examples everywhere, where the controller directly accesses the Eloquent model in order to receive data and pass it to the View. I had a suspicion that in real projects there is another layer between the controller and the model. For example, if we have a blog, and in order to get some kind of publication, we can access some class, such as Posts:get($id) and already there determine the method of obtaining the publication, for example, access the Eloquent model. Is it possible to do so? Does Laravel have any guidance on this? After all, it is only necessary to change the name of the field, table in the database and you will have to rewrite a bunch of files, and if you use any one class for this, then only in one place.
2) Models should all be in the app folder, or do you need to create a separate directory for them?
3) Where is the best place to place your classes and functions? In the documentation, an import like use App\Services\... is sometimes used as an example, but the Services folder is not in the Directory Structure section of the documentation
Answer the question
In order to leave comments, you need to log in
As you wish. The main problem is the misconception that laravel provides a ready-made structure that must be faithfully followed - and everything is just the opposite. Out of the box, it allows you to build the structure the way you want, without any consequences.
I always immediately break the application into modules (domains) and there I start from the structure I need.
Blindly transferring code from the controller to the service does not make sense, IMHO.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question