Here is what I am trying to achieve. I have a magento2 store and I want to add customization functionality. The way it is going to work is I add a link on products page which will take users to this customization page.

This customization page will have its own URL and hence will be a new module. Here is what I have so far:

  1. Controller: Does nothing, returns a page.
  2. Block: Has some sample functions defined.
  3. Templates: Has HTML code and gets the values by calling sample functions from block.

Here is what I need now:

  1. Where should my logic go. This logic will get the metadata from database for chosen product and display options. Should this be in block or the controller?

  2. I think the logic to display options will be in template. Correct me if I am wrong.


1) As in M1 the right place manage database data (assuming you'll use an existent Model/ResourceModel like products or customer) and apply its logic is the block, you should use controllers just to dispatch, maybe catch/handle success/error messages and eventually manage some ACL logic.

Is and actually always was a good practice keep controllers lightweight, simple and clean as possible.

2) The display logic could be implemented in template, as you say, but can be handled in the Block as well.

The idea is the same as in M1, use templates just to print/iterate and handle display-related stuff (like show or not some html block depending some value).

  • Thanks for your reply. I tried this, but somehow I am not able to get a connection in block. Gives me this error: Object DOMDocument should be created – sanjay Dec 30 '15 at 8:55
  • I don't see any relation between Object DOMDocument error and connection, btw if you need to make a directly call to a database you should implement that part of code in a resourceModel (not within the block) and instantiate, filter and whatever with that model in the Block. – MauroNigrele Dec 30 '15 at 9:03

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.