Magento – Show two product in home page using the custom module

blockshelpermoduleproduct

I have created my custom module. It works fine. But now i want to show two/three simple product in my home page using my custom module. can I use helper or block? which will more efficient or easy to use? If I fetch the product from my custom module block or helper page then i will call my custom helper or block in home page.Is it possible or right way to show? but i have to use my custom module. I have followed this link url
but it's not working for me, it shows 404 page. If I added controller class like

class Monojit_Custom_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
      //$helper  = Mage::helper('custom');
      //print_r($helper);
        echo "here is my module";
    }
}  

then it works fine. Can anyone please tell me how can I fixed this issue. Till now I didn't change any settings for admin->design template. I also tried to create helper (url) but unfortunately it also not worked for me.

Any help will be greatly appreciated..

Best Answer

If there is a common functionality/method which you might going to use for other modules as well, better you add those methods inside a helper class.
Now for this instance, you can use either block or helper to write method in order to retrieve a set of simple products. But my suggestion is use the block to retrieve data you want and call the block (block method) in home page.

I'll elaborate the answer here itself.
Suppose we have a module name "Home" inside a namespace as "Test".
I hope you know how to create module activation file, so I'll eliminate it here.
So the config.xml is like this:

app\code\local\Test\Home\etc\config.xml

  <?xml version="1.0"?> <config>
         <modules>
             <Test_Home>
                 <version>0.1.0</version>
             </Test_Home>
         </modules>

         <frontend>
             <routers>
                 <home>
                     <use>standard</use>
                     <args>
                         <module>Test_Home</module>
                         <frontName>home</frontName>
                     </args>
                 </home>
             </routers>
             <layout>
                 <updates>
                     <home>
                         <file>home.xml</file>
                     </home>
                 </updates>
             </layout>
         </frontend>
         <global>  
             <blocks>
                 <home>
                     <class>Test_Home_Block</class>
                 </home>
             </blocks>
          <models>
                 <home>
                     <class>Test_Home_Model</class>
                 </home>
             </models>    
         </global>  
         <admin>
             <routers>
                 <home>
                     <use>admin</use>
                     <args>
                         <module>Test_Home</module>
                         <frontName>home</frontName>
                     </args>
                 </home>
             </routers>
         </admin> 
      </config>

And the block file is like below:

app\code\local\Test\Home\Block\Featuredproducts.php

<?php

class Test_Home_Block_Featuredproducts extends Mage_Core_Block_Template{

    public function _construct() {     
        parent::_construct(); 
        $this->setTemplate('test/featured_products.phtml');     
    }
        public function getFeaturedProducts()
        {
            $collection = Mage::getModel('catalog/product')->getCollection()
                                        ->addAttributeToSelect("*")
                                        ->addFieldToFilter('status','1')
                                        ->addFieldToFilter('visibility',array('in' => array(2,4))); // 2 = Search, 4 = Catalog, Search              
            return $collection;
        }
    }

Inside template file name as "featured_products.phtml"

app\design\frontend\default\home\template\test\featured_products.phtml

Now we can call getFeaturedProducts method which is written on the block.

$product_collection = $this->getFeaturedProducts();

Hope this answer will help you.