M
M
maxandush94942020-01-08 03:55:32
Laravel
maxandush9494, 2020-01-08 03:55:32

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

2 answer(s)
A
Alex Wells, 2020-01-08
@Alex_Wells

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.

Z
zorca, 2020-01-08
@zorca

  1. Yes. It is called Repository - an additional layer of abstraction that does not depend on a specific data storage method.
    In general, an example of good project structuring practices can be seen in Laravel Boilerplate .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question