Question

Is it considered good/bad architecture to set up a helper and let this handle all the constants, variables and data?

I am currently writing a fairly simple extension to do with member/non-member logins - mostly frontend stuff eg. 'remember me' to be checked in certain cases, login box to be open in certain cases, etc. Using cookies quite extensively for this and no real database access with very minimal config options via admin.

Was it helpful?

Solution

"Constants, variables and data", those sound to me like 'model' things.

Generally, the answer is going to really depend on the specific problem you're trying to solve. But I tend to encapsulate these types of things in a model - even if it does not need to use the database.

Now remember, a model represents your domain (where domain means business/applications logic) and not just data persistence. This can be confusing, as these days people see 'model' and immediately think database.

The exception to this rule is when you need to access behaviour from a block or template. It's very convenient to use helpers:

$this->helper('foomodule')->formatRefererenceNumber($myModel->getReferenceNumber())

This is much more pleasant (and computationally efficient) than the alternative

Mage::getModel('foomodule/model')->formatReferenceNumber($myModel->getReferenceNumber()

Of course, you might think shouldn't formatReferenceNumber just be a method on $myModel then? And possibly, yes. But having it in a helper lets you separate the concern of formatting a reference number from the storage of it. You can easily specify alternative formatting options - possibly across different helpers - without weighing down your model.

The other time you want to use helpers over models is if you find yourself writing lots of PHP code in templates. Let's say you have a table of rows and you want odd rows to be one colour and even rows another. I extract that sort of functionality into a helper.

Generally the heuristic I follow is to keep business data and logic in models, and use helpers to transform data into specific forms for presentation on the frontend.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top