Answer the question
In order to leave comments, you need to log in
Where to place helper classes in mvc architecture?
I recently wrote a simple mvc framework in php from video tutorials, I seem to understand the essence, the architecture is really well thought out and visual, but I decided to make a web application based on this framework and fell into a stupor about one question. The bottom line is that there is such a folder structure:
Let's say I have my own controller for a certain address, it's clear that it is located in the App / Controllers folder, but what if the logic of this controller contains instances of any auxiliary classes, that is, where to store the files of these classes? Placing several classes in one controller file is not comme il faut, and interfering with auxiliary class files in the folder with controllers also does not look like a good solution. I decided to see how this is done in normal projects, but I did not find similar classes in them. In general, I could place them anywhere and use them safely, but my question is exactly how it is customary to do this, what are the best practices in this matter?
Answer the question
In order to leave comments, you need to log in
There are several solutions to this problem.
1) Some may promote the service layer ( design-pattern.ru/patterns/service-layer.html), as a consequence: "App/Services/ServiceName" - a set of classes responsible for some abstract action. For example: "UserAvatarUploaderService", which is responsible for uploading the user's avatar.
2) Alternatively, and the way I prefer, is to create a "src" directory with a set of independent components, including its own composer.json. The same avatar upload service would look like this:
- src/
- AvatarUploader/
- README.md
- composer.json
- tests/ ...
- src/
- AvatarInterface.php
- FileSystemInterface.php
- FileSystemUploader.php
- UploaderInterface.php
{
"repositories": [
{
"type": "path",
"url": "./src/AvatarUploader"
}
],
"require": {
"app/avatar-uploader": "*"
}
}
{
"name": "app/avatar-uploader",
"autoload": {
"psr-4": {
"AvatarUploader\\": "src/"
}
}
}
If you are working with the database, then it is better to create a class in the models, and you can name it whatever you like, for example Helper
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question