Are helper classes required for all Magento extensions?

Many extensions (including those written by me) include a helper class that simply extends the abstract base class without adding any functionality. The helper usually looks like this:

class MyCompany_MyModule_Helper_Data extends Mage_Core_Helper_Abstract { } 

Therefore, an extended class is used only for things that an abstract class provides, especially for translations. On the other hand, all the Block and Controller classes in Magento inherit the __() method for translations - and in the extension I'm currently developing, I don’t need to call a helper class even once.

Is it possible to remove the helper class and remove it from config.xml ? I tried this, and the extension seems to work fine, but due to the complexity of Magento, I'm always a little worried that there are consequences that I don't know about.

+6
source share
2 answers

If you create a module from scratch, helper classes are not strictly necessary. I usually skip the creation until it is needed.

However , if any XML file uses the module attribute to indicate the translation module, this attribute must be enabled for a valid helper. For example, in this main file

 <!-- File: app/code/core/Mage/Catalog/etc/system.xml --> <tabs> <catalog translate="label" module="catalog"> <label>Catalog</label> <sort_order>200</sort_order> </catalog> </tabs> 

There module="catalog" . By specifying this attribute, the Magento system code that translates the label will look something like this.

 Mage::helper('catalog')->__('Label'); 

So, removing the helper from the directory module will break the parts of Magento.

(The class attribute of a single part of the catalog automatically converted to Mage::helper('catalog/data') by the Magento system code)

This "translation assistance system.xml " function is used in many Magento XML files, not just system.xml (layout, widgets, etc.). In addition, Magento has several systems that will output and / or require an auxiliary module for translations (Access Control, external API system, etc.).

Long Story Short: if you are creating a module from scratch, feel free to leave an assistant until you start getting errors that Magento cannot create an assistant. Never remove an existing helper from a module, and if you want to make sure that you are 100% compatible with the assumptions that other people can make, always include the helper class Data.php .

+9
source

The Magento Helper classes contain utilities that let you perform common tasks on objects and variables. http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-1-introduction-to-magento#6

Assuming the Helper file is empty without special methods, for example.

 <?php class MagePal_SomeModule_Helper_Data extends Mage_Core_Helper_Abstract { } 

Then some of the things that may be affected are as follows:

  • system.xml - a blank screen for your module in admin -> system -> config
  • $ this β†’ __ ('') - an error in your .phtml template (for internationalization / translation)

So, if your assistant is empty, without a system configuration section and without translation, you can "delete" it.

+4
source

All Articles