What is helper in Magento?
In what cases should one use and not use helpers?
Theoretically you should never use helpers.
Helpers are just collections of unrelated methods and are always instantiated as a singletons.
This is basically procedural programming with functions grouped under some namespace (the class name in this case).
But since Magento has helpers in the core you can put your methods in there that you have no idea where to put them or if you need to call them in a lot of different places (models, controllers, templates)
Use them as a last resort.
Also Magento requires a helper for each module for translation reasons.
You can just create a helper called Data.php
in each module and leave it empty.
foreach
loops and all sorts of madness. I found re factoring this terrifying logic to a helper and using it as an object cache to be useful, and left little room for errors from future developers who may have accidentally call getModel
instead of getSingleton
if I'd placed it in a model.
Commented
Jul 20, 2015 at 10:35
The question has two aspects:
In general, having classes named Helper
, Util
or similar just says "I have some functions that I don't know where to put" and don't make much sense as a class.
Magento instantiates helpers as singletons and most of the core helpers don't have any state, so the methods could as well be static
or even functions
without a class. All of this is often considered a code smell, a flaw in the application design.
As Marius already pointed out, you don't need to use helpers for your own code. Just create a default empty helper per module if you use module specific translations, otherwise they won't work. Prefer models (which do not need to extend Mage_Core_Model_Abstract
if they don't represent database data) or independent library classes.
However, I would not be too strict about "not using helpers at all" and instead use them for query shortcuts like:
access module configuration:
public function getFooBar()
{
return Mage::getStoreConfig('module/foo/bar');
}
factory methods for library classes
public function getNewFooService()
{
return new \Foo\Service(...);
}
You could find other places but IMHO, the module helper is often good enough for things like that.
Consuming the core helpers is something you will do quite often.
__()
translation method: To get a translation of a specific module, you should use Mage::helper('module-alias')->__('string to be translated')
. This happens implicitly if you use $this->__(...)
within a template or block and if you use the translate="..."
attribute in XML filesMage::helper('core')
methods: localized date, price and currency formating, escaping and encoding dataMage::helper('tax')
methods to get information from the tax configuration and calculate prices based on thatMage::helper('catalog/image')
provides an interface to create cached and resized catalog images and retrieve their URLMage::helper('catalog/product_url_rewrite')->joinTableToSelect()
joins the URL rewrite table to a product collection query.There are many more (more or less) useful functions hidden in the core helpers, if you need a specific functionality that is likely to be used in the core somewhere, check if you can reuse a helper method.
Usually these helpers are stateless objects and the methods are query methods (i.e. they have no side effects)
But as always Magento breaks its own unwritten rules and should not be taken as an example. A "good" example how to not use helpers is Mage_Catalog_Helper_Product_Compare
which has an $_itemCollection
property that can only be initialized once and a $_customerId
property that can be changed with a setter. You will find a few more helpers related to the catalog with attached collections. Writing tests for code that uses them or re-use them in different context is not fun, so please don't do that at home.
The catalog/image
helper mentioned above is another example of a helper that really should not be a helper. You need to pass a product with init()
first which resets its current state, then you set various parameters (like resize()
, setQuality()
) and in the end you can get the URL with its __toString()
method. That looks nice when used in a template but the code is a huge mess and it doesn't make sense as a singleton.
TL;DR:
Reader
and Writer
models, that actually do have state (at least a file resource). For example, for reading order status data from a CSV file I would have sth. lika a OrderStatusCsvReader
model that is used by a OrderStatusUpdater
model. This way I also separate the concerns "read data from file" and "update order in Magento"
Commented
Jul 21, 2015 at 6:21
Marius is right. I think helpers are nonsense.
But in magento theory you should put everything into helpers which doesn't change the state of an object, e.g. get formatted price.
But everything you can put into a helper, you can put into a model too. And you can get different instances of a model, which is helpful for testing.
I am quite new to Magento, but to me it looks like a Helper is Magento's equivalent of a service: "a set of related software functionalities that can be reused for different purposes". A module exports its offered functionality through services. Use a helper for the functions that you invite other modules to use.
A model should only provide methods that are directly related to getting or setting an object's state, or that are otherwise linked to the instantiated object of the model.
Helps are useful to prevent duplicated code (in models, templates, ...) and sometime they are just necessary.
Mage::getStoreConfigFlag('my/module/enabled')
to every file where you want to check this, or you use Mage::helper('my_module')->isEnabled()
with benefits:
isEnabled()
method and it will affect all classes that use it, instead of rewriting several filesMage_Catalog_Model_Product
to add the method getProductArticles()
. Right. In your helper add getProductArticles(Mage_Catalog_Model_Product $product)
<action method="someMethod"><var helper="module/method" /></action>
You can just create a helper called
Data.php
in each module and leave it empty.
When using PHPUnit you should add one single line: protected $_moduleName = 'My_Module';
A helper in Magento is a class created to simplify everyday tasks and provide reusable code snippets. Helpers are usually used when code needs to be shared across multiple modules or when multiple modules need access to the same functionality. They are usually used to perform tasks specific to a particular module, such as formatting data or transforming values.
One should use helpers when code needs to be shared across multiple modules or when multiple modules need access to the same functionality. One should not use helpers when the task is too specific to a single module and does not need to be shared across multiple modules.