0

I would like to ask for example I have a function doSomething() which will be used by one or more other files, where is the best place to put it?

As of now, I put everything in Data.php (helper class) and everything is public.

I've read here that using helper class is not a good idea.

If these functions are important and I want them to be secured, what is the best way to achieve this?

Thank you.

1

Magento is based on MVC system, and the MVC doesn't only contains helper files, each file have it's path and it's usefulness.

Model: represents your data structures, Typically your model classes will contain functions that help you retrieve, insert, and update information in your database, also ORMs (Object Relational Mapping) get you out of the business of writing SQL and allow you to manipulate a datastore purely through PHP code.

Block: Magento blocks are used to pass data to phtml, that this block gets it's data from Model.

Helper: helper function is a small convinience function that doesn't really belong somewhere that helps you to do something you can already do, albeit with less typing. An overload that forwards with the usual defaults for example.

Magento's Helper classes contain utility methods that will allow you to perform common tasks on objects and variables.

Observer: Like any good object-oriented system, Magento implements an Event/Observer pattern for end users to hook into. As certain actions happen during a Page request (a Model is saved, a user logs in, etc.), Magento will issue an event signal.


Conclusion:

I think you have understood the usefulness of each Magento files which I noted above (There are also other files). So There is not one place better than the other to put your function, it depends on the use and usefulness of your function, what is the purpose and where it will be used.

For exemple: If your function handle data database informations, then you display them in phtml, I tell you to create your function in block ex: public function getCustomData() in this function you get your model inside..., afterthat you get the result of your getCustomData() function in phtml like this : $this->getCustomData().

More information in Magento Devdocs.

Good luck.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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