0

I cannot seem to find an official information about what is the recommended way to retrieve configuration values in Magento 2.

I am searching for the recommended way to retrieve values from the configuration. Should it be from a Helper, or from a "Config" Model ?

In the Magento official documentation about best practices, they talk about avoiding to create helper classes, but it is more about static methods.

In the Magento code, we can see both are used, and even sometimes the config values are retrieved directly from other classes along with other code logic.

Here are some examples of both usages :

What is your approach on this matter ? And why ?

I will share my own approach about it, and am open to comments to discuss about it.

1
  • Great ! I thinks I will use Models instead of Helpers for the next developpement if it's ligher ! Thanks.
    – Gangdhi
    Oct 16, 2023 at 13:17

1 Answer 1

0

Helper class usage

Here are some observations about Helper usage to retrieve config :

  • They seem to be older, maybe it is a (sometimes bad) habit from Magento 1 with its data helpers, which has been kept at the begining of Magento 2.
  • They often had the @api tag early in the Magento project. If I am not mistaken, @api tag means that the class is safe to use, as it should not have backward incompatible changes. This might be another reason why they are still here.
  • They are often surrounded by additional code logic, which makes their purpose to other things than just "get the config".
  • Helpers also look heavier, because they usually extend "AbstractHelper" which is requiring a lot of dependency injections and goes against the composite reuse principle (Composition over inheritance).

Config Model class usage

Here are some observations about Config Model usage to retrieve config :

  • It looks like new config classes tend to be Models instead of Helpers, making it look like it is a preferred solution than the Helper.
  • A Config Model has its own purpose about retrieving the configuration values from the database (or cache), which makes sense about what a model is supposed to do.
  • They generaly come with their own Inteface, mapped in the di.xml file for an abstract implementation, which is a good thing.
  • Using this kind of Model also seems lighter, because it favors composition over inheritence, which is a good practice.

Conclusion

With all those observations in mind, I would tend to use Models instead of Helpers.

It looks cleaner, lighter, and seems to follow more coding standards.

5
  • FYI: Avoid creating helper classes. Helper or utility classes are classes filled with static methods that do not quite fit anywhere else. These classes are considered an anti-pattern and violate the principles of object-oriented programming. A helper class that functions as a catch-all for random methods violates the single responsibility principle because it is an attempt to solve multiple problems in a single class. You should refactor your code and move those functions into the appropriate classes. developer.adobe.com/commerce/php/best-practices/extensions
    – Tu Van
    Oct 5, 2023 at 13:42
  • @TuVan Yep, that's what I stated in the question. But in the case of getting config, we are neither using static methods, nor random methods as they have all the same purpose. This kind of make those helper having a single responsibility. That's why I'm not 100% sure that they should not be created anymore.
    – Cladiuss
    Oct 6, 2023 at 10:01
  • 1
    as a complement i would say that helper tended to be used in template which was a bad practice. So to fix that usually what we do is to call the helper in the block of the template; in the specific case of getting a config value...we can agree that a config is related to an object so use it in a model makes way more sense to me. Thought when calling a config not related to a model i don't necessary think it's a bad thing to have a global helper or a block for example. As a conclusion i would say it all the depend on which data you want to recover
    – Claims
    Oct 16, 2023 at 14:19
  • @Cladiuss @Claims: Because Magento 2 is a large application, there are still bad practices that are inherited from Magento 1. Regarding getting config values, we should directly inject \Magento\Framework\App\Config\ScopeConfigInterface into the specific class we are working on, whether Model, Controller, Block, ViewModel, Plugin, Observer, Cron, Console Command, UI Data Provider, Setup, and so on. To verify that, you can search scopeConfig-> throughout the entire Magento codebase, which is located in the vendor/magento directory.
    – Tu Van
    Nov 1, 2023 at 4:29
  • @Cladiuss please accept your answer to mark the question as solved. Cheers!
    – Tu Van
    Nov 1, 2023 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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