skip to Main Content

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


  1. If you want to product name on phtml then.

    1. If you have multiple product on page, then put code in phtml file it self.

    2. If you have single product then you can used Block file to load product.

    Login or Signup to reply.
  2. 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:

    1. custom module skeleton
    2. frontend controller to load the layout file when a URL is called
    3. an xml layout file to declare the custom block you will be using & .phtml template you will be using
    4. your own block, so you can call your custom function from the .phtml file
    5. a .phtml file to output the name of the returned Product object, e.g if you have “loadMyProduct()” function in the block class, you will just call it $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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search