4

I've created a Magento 2 module and I set up a class that extends Magento\Framework\DataObject that I'm using to pass data between my helper (e.g. Package\Module\Helper\Data) and model (e.g. Package\Module\Model\Total\Checkout\Total).

It's just nicer syntax then passing an array back and forth, allows me to keep my code DRY, and adds some free functionality (like toJson()).

For now I've placed it in the Model directory (e.g. Package\Module\Model\Data\MyData).

My question is: Where is the best place to put a class that doesn't seem to belong in any of the traditional module folders? Its location should make it's role apparent to the next developer who works on this code before they even look at its contents. Is there an existing construct?

5

From a "traditional" Magento perspective, it would be model. Note that not every model extends the abstract model class and has a resource model.

But in Magento 2 we are not limited anymore to the "Helper", "Model" and "Block" namespaces.

There are a few more "standard" namespaces like "Observer" and "Plugin", but you are free to use your own and structure your module as it makes sense to you.

I've used Model/Data myself for data structures, but then again I also used Model/Plugin, Model/Observer and so on, because I was used to the old structure.

It probably makes sense to get rid of Model for those, and use Model only for "real" models, but I think it's a matter of taste. Even in the "Customer" module which is supposed to be an example for the new architecture, it is not consistent.

5

I don't think it's really important where you put this.
You can even think of your own folder where to put this.
For example, in the catalog module there is the folder Pricing that contains classes related to price "stuff" (Duh!).
If you don't feel comfortable adding it to the Model folder you can do the same.
I would create a folder called Data or Framework or anything else and place it there.

  • Or simply Class or Utils – Raphael at Digital Pianism Oct 5 '16 at 15:06
  • I would stay away from folder names that are reserved words. Like class – Marius Oct 5 '16 at 15:10
  • That makes sense to me: Package\Module\Utils\Data\MyData. I'm going to see if any more opinions trickle in before marking it answered. – jared_flack Oct 5 '16 at 15:19
  • 1
    @jared_flack. Sure. I would love to see other opinions. Specially from a core team member. – Marius Oct 5 '16 at 15:20

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.