Let’s say that I want to load a product and show its name (or delete it). Talking about Best Practise, is it better to create the “load” method in a block or a controller ?
Let’s say that I want to load a product and show its name (or delete it). Talking about Best Practise, is it better to create the “load” method in a block or a controller ?
2
Answers
If you want to product name on phtml then.
If you have multiple product on page, then put code in phtml file it self.
If you have single product then you can used Block file to load product.
The question needs a bit more background as to where you would actually be displaying this item to help you better. In order to understand the best practices of Magento at the beginner level you should always take a look at the Magento Core logic to see what they normally do.
As for the “Best Practice” there are quite a few things you will need to get this to work in Magento:
$product = $this->loadMyProduct();
in the .phtml file.After that, you can access the
$product->getName()
and other properties in the .phtml file.P.S. If your load product function gets more complicated, an even better practice is to have your code in a Helper class and call the helper from the Block class 😀
Please see Alan Storm’s guide on this topic in full detail.