J
J
John Bjornsen2018-12-27 19:22:30
PHP
John Bjornsen, 2018-12-27 19:22:30

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:
5c24f7ceb87b3707891508.png
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

5 answer(s)
K
Kirill Nesmeyanov, 2018-12-27
@Bjornsen

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

To connect this library, it will be enough to register in the root:
{
    "repositories": [
        {
            "type": "path",
            "url": "./src/AvatarUploader"
        }
    ],
    "require": {
        "app/avatar-uploader": "*"
    }
}

And the internal (i.e. inside "src/AvatarUploader") composer will look like this:
{
    "name": "app/avatar-uploader",
    "autoload": {
        "psr-4": {
            "AvatarUploader\\": "src/"
        }
    }
}

Thus, we will write down a completely independent component with its own tests and dependencies, and inside the main application we will only refer to it.

A
Adamos, 2018-12-27
@Adamos

/vendor/{your_company}/{project}?

D
Dmitry Dart, 2018-12-27
@gobananas

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

D
dmitriy, 2018-12-27
@dmitriylanets

in your case by class types: Helpers, Configs, Exceptions,
Classes that are generic and not related to the application, usually in /vendor/{your_company}/{project}
as mentioned above

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question