0

I want to add custom block with custom logic to customer account. Is there any preferable inheritance in this case fro new block?

0

It depends on what you need.
For a simple text block \Magento\Framework\View\Element\Template should be enough.
If you add something in the dashboard you might want to use \Magento\Customer\Block\Account\Dashboard as a parent class. It already contains methods for retrieving the current customer, the account url, the address url.

2
  • I thought that there something maybe in sake of security and it is recommended to extend for. I have to prepare additional block with recently visited product for customer and it should be visible inside customer account. – Bartosz Kubicki Jan 10 '17 at 13:48
  • I think you can simply extend the template block and add a method in there that retrieves the current customer. The security part should be implemented in the controller. So if you use the block only in the customer account page you should be safe. – Marius Jan 10 '17 at 13:50
2

Do not use inheritance, if it is not necessary.

Moreover, blocks are private classes of modules, this class may be modified in next versions of module.

Should not use extending \Magento\Customer\Block\Account\Dashboard for using only one method. This class have many dependencies, which not needed in your block. Inheritance will increase coupling between objects, arguments count, stability in your class and your module will become non-reliable.

Use inheritance from \Magento\Framework\View\Element\Template and implement only the necessary methods for you

1
  • +1 for pointing out drawbacks of inheritance from \Magento\Customer\Block\Account\Dashboard – Bartosz Kubicki Jan 10 '17 at 19:00

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.