How to make a module to interact with database in magento

How to make a module in Magento to show data in front end.

Step 1:-

Go to your magento root directory   and create File’s and folders:-

1.       Create a xml configuration file in magento\app\etc\modules\Summer_Spring.xml.
2.       Create a namespace Folder under named “Summer” where “Summer” is namespace for our module magento\app\code\local\Summer.
3.       Create a Module folder under our namespace folder named with Spring so now we have module magento\app\code\local\Summer\Spring.
4.       Under our module we have to create five new folder’s  and relative files:-
 A)  etc/config.xml.
 B) controller/ IndexController.php     (controller name should start in lower case).
C)   Block/Index.php   (Block name should start in lower case).
D)  Helper / Data.php  Block name should start in lower case).
E)  Model/ Spring.php  Block name should start in lower case).
F)  Model/ Mysql4/ Spring.php
G)  Model/ Mysql4/ Spring/ Collection.php
5.       Under  Your theme section you have to create a config file For template. So create this file in magento\app\design\frontend\<Your theme package >\<Your theme>\layout\spring.xml
6.       Now create a folder in magento\app\design\frontend\<Your theme package >\<Your theme>\template\spring
7.       Now under “spring” create a file index.phtml
Ok now we have created all the files and folders for our Module. So let’s start coding
8.       First Open Summer_Spring.xml. and add the following code in this file and save it

<!---  Code start From here - ->
<?xml version="1.0"?><config>  <modules>    <Summer_Spring>      <active>true</active>      <codePool>local</codePool>      <version>0.1.0</version>    </Summer_Spring>  </modules></config><!---  Code End here - ->
Note :- Save this File        

9.       Now Go to magento\app\code\local\Summer\Spring\etc And open “config.xml” File and following Code:-
<!---  Code start From here - ->
<?xml version="1.0"?><config>  <modules>    <Summer_Spring>      <version>0.1.0</version>    </Summer_Spring>  </modules>  <frontend>    <routers>      <spring>        <use>standard</use>          <args>            <module>Summer_Spring</module>            <frontName>spring</frontName>          </args>      </spring>    </routers>                                <layout>                                  <updates>                                                <spring>                                                  <file>spring.xml</file>                                                </spring>                                  </updates>                                </layout>  </frontend>  <global>    <helpers>      <spring>        <class>Summer_Spring_Helper</class>      </spring>    </helpers>                <blocks>                  <spring>                                <class>Summer_Spring_Block</class>                  </spring>                </blocks>                <models>                  <spring>                                <class>Summer_Spring_Model</class>                                <resourceModel>spring_mysql4</resourceModel>                  </spring>                  <spring_mysql4>                                <class>Summer_Spring_Model_Mysql4</class>                                <entities>                           
                                                  <spring><!—put your table name under <table>   à                                                                <table>apr_arun</table>                                                  </spring>        </entities>                  </spring_mysql4>                </models>                <resources>                  <spring_setup>                                <setup>                                  <module>Summer_Spring</module>                                </setup>                                <connection>                                  <use>core_setup</use>                                </connection>                  </spring_setup>                  <spring_write>                                <connection>                                  <use>core_write</use>                                </connection>                  </spring_write>                  <spring_read>                                <connection>                                  <use>core_read</use>                                </connection>                  </spring_read>                </resources>  </global></config>

<!---  Code End here - ->
Note :- Save this File        
10.   Now Go to magento\app\code\local\Summer\Spring\controller And open “IndexController.php” File and following Code:-
<!---  Code start From here - ->
<?phpclass Summer_Spring_IndexController extends Mage_Core_Controller_Front_Action{    public function IndexAction() {    
                  $this->loadLayout(); 
                  $this->renderLayout();
                
    }}
?><!---  Code End here - ->
Note :- Save this File

11.   Now Go to magento\app\code\local\Summer\Spring\Helper And open “Data.php” File and following Code:-
<!---  Code start From here - ->
<?phpclass Summer_Spring_Helper_Data extends Mage_Core_Helper_Abstract{}                ?>
<!---  Code End here - ->
Note :- Save this File

12.   Now Go to magento\app\code\local\Summer\Spring\Block And open “Index.php” File and following Code:-
<!---  Code start From here - ->
<?php 
class Summer_Spring_Block_Index extends Mage_Core_Block_Template{ 
      public function mymodblockfun()     {      
       $retour='';                                $collection = Mage::getSingleton("spring/spring")->getCollection();        foreach($collection as $data)        {             $retour .= $data->getData('first_name').' '.$data->getData('last_name').' '.$data->getData('email_id').'<br />';         }                                 return $retour;      }}?><!---  Code End here - ->
Note :- Save this File.

13.   Now Go to magento\app\code\local\Summer\Spring\Model And open “Spring.php” File and following Code:-
<!---  Code start From here - ->
<?php 
class Summer_Spring_Model_Spring extends Mage_Core_Model_Abstract {    protected function _construct(){       $this->_init("spring/spring");    }}    ?>
<!---  Code End here - ->
Note :- Save this File.
14.   Now Go to magento\app\code\local\Summer\Spring\Model\ Mysql4 And open “Spring.php” File and following Code:-
<!---  Code start From here - ->
<?php 
class Summer_Spring_Model_Mysql4_Spring extends Mage_Core_Model_Mysql4_Abstract{
  protected function _construct()    {        $this->_init("spring/spring", "id");    }}   ?>
<!---  Code End here - ->
Note :- Save this File.

15.   Now Go to magento\app\code\local\Summer\Spring\Model\ Mysql4\Spring And open “Collection.php” File and following Code:-
<!---  Code start From here - ->
<?phpclass Summer_Spring_Model_Mysql4_Spring_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract    {                                public function _construct(){                                                $this->_init("spring/spring");                                }    }                  ?><!---  Code End here - ->
Note :- Save this File.

16.   Now Go to magento\app\design\frontend\<Your theme package >\<Your theme>\layout\  And open “spring.xml”  File and following Code:-
<!---  Code start From here - ->
<?xml version="1.0"?> 
<layout version="0.1.0"> 
  <spring_index_index> 
    <reference name="root"> 
      <action method="setTemplate"><template>page/1column.phtml</template></action> 
    </reference> 
    <reference name="content"> 
      <block type="spring/index" name="spring_index" template="spring/index.phtml"/> 
    </reference> 
  </spring_index_index> 
</layout> 
<!---  Code End here - ->
Note :- Save this File.

17.   Now Go to magento magento\app\design\frontend\<Your theme package >\<Your theme>\template\spring And open “index.phtml”  File and following Code:-
<!---  Code start From here - ->
<?phpecho $this->mymodblockfun();?><!---  Code End here - ->
Note :- Save this File.
Ok now we have crated the module go to front end and type exp  www.magento/spring/indexThat’s it.
 

No comments:

Post a Comment