Beginners learn Laravel in two ways. One is to obediently fill the program into the MVC framework, resulting in abnormal enlargement of controller and model, which will be difficult to maintain in the future. One is that you often don’t know which class to write your program in, because traditional PHP is a page per file. This article presents a medium to large project architecture that is most suitable for Laravel, easy to maintain, easy to scale, easy to reuse, and easy to test.
Version
Laravel 5.1.24
Controller is too big
Due to RoR, beginners often think of MVC architecture as model, View, controller:
- A Model is a database.
- The Controller is responsible for communicating with HTTP, calling model and View.
- View is HTML.
If you follow this definition, where do these requirements go?
- Send Email, use external API.
- Logic written in PHP.
- Convert the display format as required.
- Whether to display certain data on demand.
- Display different information as required.
Among them, 1 and 2 belong to business logic, while 3, 4 and 5 belong to display logic. According to the common definition of MVC, model is database, and view is HTML, all the above requirements can not be written in model and View, but only in controller.
Therefore, beginners begin to write a large number of programs in the controller, resulting in the controller is too large to maintain.
Model is too big
When you move logic from Controller to Model, controller gets thinner but Model gets fatter, and model goes from representing databases to being burdened with business logic and display logic, and the results get even worse.
Does Model represent a database? Think of it as the Eloquent Class. The database logic should be stored in repository, which is why Laravel 5 no longer has a Models directory, just the root of the app, Eloquent Class.
Medium to large project architecture
So how do we write that? Don’t limit our thinking to MVC:
- Model: Just use it as the Eloquent class.
- Repository: Assists the model, handles the database logic, and then injects it into the Service.
- Service: Secondary controller, handles business logic, and then injects into controller.
- Controller: receives HTTP requests and invokes other services.
- Presenter: Handles the display logic and then injects it into the view.
- View: Binding data to HTML using blade.
Blue is the original MVC, and purple is the focus of this article: Repository, Service, and Presenter patterns.
The arrow indicates the direction of object dependency injection. 1 1 For details about dependency injection, see Discussing dependency Injection in Depth
We can see that the MVC architecture is still in place due to SOLID’s single responsibility principle and dependency inversion principle:
- We separated the database logic from the Model, assisted the Model by repository, and injected the Model dependencies into repository.
- We separate the business logic from the Controller, with service assisting the Controller and injecting the Service dependency into the Controller.
- We separate the display logic from the View, give presenter assistance to the view, and inject presenter dependencies into the view.
Build directory
inapp
Directory creationRepositories
.Services
與 Presenters
Directory.
Don’t be afraid to create directories other than the Laravel default directory. According to SOLID’s single responsibility principle, the more class features, the more responsibilities, and therefore the more violations of the single responsibility principle, so you should break up your program into smaller parts, each with its own unique functionality. Instead of a single class, you should not have just three MVC parts. Create a directory for your needs and put the appropriate classes in that directory, as long as our class has a namespace to help us classify them.
Repository
Due to the lack of space, a separate article will discuss repository. Please refer to how to use the Repository schema.
Service
Due to the lack of space, service will be discussed in a separate article. Please refer to how to Use service Mode.
Presenter
Due to the lack of space, presenter will be a separate article for discussion. Please refer to how to use Presenter mode?
Unit testing
Because model, View, and Controller dependencies are now separated and use dependency injection, each part can be unit tested separately. Mock repository to test a Service. You can mock other services as well.
A Presenter can also run unit tests alone and mock other services without having to run acceptance tests to test the display logic.
Conclusion
-
The architecture discussed in this article is just the beginning. You can add as many directories and classes as you need. When you find that your MVC is violating the SOLID principles, you can take the class apart and refactor it.
- Create a new class or interface.
- Inject dependent object dependencies into class.
- Handles his responsibilities within class.
- Inject a class or interface into a Controller or view.
-
Finally, unit tests are used to test whether the refactored architecture results in the same way as the original requirements.