Sunday 1 April 2012

Create simple Module In Back End In magento

 Create simple Module In Back End In magento



thanks to :
http://www.webspeaks.in/2010/08/create-your-first-adminbackend-module.html


Step 1: Declare your shell module and it’s code pool
Create an xml file /app/etc/modules/Company_Web.xml (You can use any name, even you can use a single file to declare number of modules).
<?xml version="1.0"?>
<config>
    <modules>
        <Company_Web>
            <active>true</active>
            <codePool>local</codePool>
        </Company_Web>
    </modules>
</config> 

Step 2:
Create the basic directory structure under /app/code/core/local/ :-
Company/
|–Web/
| |–Block/
| |–controllers/
| |–etc/
| |–Helper/
| |–sql/
|

Step 3:
Write the front controller in app\code\local\Company\Web\controllers\IndexController.php
<?php
class Company_Web_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
  $this->loadLayout();     
  $this->renderLayout();
    }
}

Step 4:
Write your backend module controller in app\code\local\Company\Web\controllers\Adminhtml\WebController.php
<?php

class Company_Web_Adminhtml_WebController extends Mage_Adminhtml_Controller_action
{

 protected function _initAction() {
  $this->loadLayout()
   ->_setActiveMenu('web/items')
   ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
  
  return $this;
 }   
 
 public function indexAction() {
  $this->_initAction()
   ->renderLayout();
 }
}

Step 5:
Write the frontend block file in app\code\local\Company\Web\Block\Web.php
<?php
class Company_Web_Block_Web extends Mage_Core_Block_Template
{
 public function _prepareLayout()
    {
  return parent::_prepareLayout();
    }
    
     public function getWeb()     
     { 
        if (!$this->hasData('web')) {
            $this->setData('web', Mage::registry('web'));
        }
        return $this->getData('web');
        
    }
}

Step 6: Now write the following file- app\code\local\Company\Web\Block\Adminhtml\Web.php
<?php
class Company_Web_Block_Adminhtml_Web extends Mage_Adminhtml_Block_Widget_Grid_Container
{
  public function __construct()
  {
    $this->_controller = 'adminhtml_web';
    $this->_blockGroup = 'web';
    $this->_headerText = Mage::helper('web')->__('Item Manager');
    $this->_addButtonLabel = Mage::helper('web')->__('Add Item');
    parent::__construct();
  }
}

Step 7:
Create the config file as app\code\local\Company\Web\etc\config.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Company_Web>
            <version>0.1.0</version>
        </Company_Web>
    </modules>
    <frontend>
        <routers>
            <web>
                <use>standard</use>
                <args>
                    <module>Company_Web</module>
                    <frontName>web</frontName>
                </args>
            </web>
        </routers>
        <layout>
            <updates>
                <web>
                    <file>web.xml</file>
                </web>
            </updates>
        </layout>
    </frontend>
    <admin>
        <routers>
   <web>
    <use>admin</use>
    <args>
     <module>Company_Web</module>
     <frontName>web</frontName>
    </args>
   </web>
        </routers>
    </admin>
    <adminhtml>
  <menu>
   <web module="web">
    <title>Web</title>
    <sort_order>71</sort_order>               
    <children>
     <items module="web">
      <title>Manage Items</title>
      <sort_order>0</sort_order>
      <action>web/adminhtml_web</action>
     </items>
    </children>
   </web>
  </menu>
  <acl>
   <resources>
    <all>
     <title>Allow Everything</title>
    </all>
    <admin>
     <children>
      <Company_Web>
       <title>Web Module</title>
       <sort_order>10</sort_order>
      </Company_Web>
     </children>
    </admin>
   </resources>
  </acl>
  <layout>
   <updates>
    <web>
     <file>web.xml</file>
    </web>
   </updates>
  </layout>
    </adminhtml>   
    <global>
        <models>
            <web>
                <class>Company_Web_Model</class>
                <resourceModel>web_mysql4</resourceModel>
            </web>
            <web_mysql4>
                <class>Company_Web_Model_Mysql4</class>
                <entities>
                    <web>
                        <table>web</table>
                    </web>
                </entities>
            </web_mysql4>
        </models>
        <resources>
            <web_setup>
                <setup>
                    <module>Company_Web</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </web_setup>
            <web_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </web_write>
            <web_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </web_read>
        </resources>
        <blocks>
            <web>
                <class>Company_Web_Block</class>
            </web>
        </blocks>
        <helpers>
            <web>
                <class>Company_Web_Helper</class>
            </web>
        </helpers>
    </global>
</config>


Step 8: Now write the helper class app\code\local\Company\Web\Helper\Data.php
<?php

class Company_Web_Helper_Data extends Mage_Core_Helper_Abstract
{

}

Step 9: Create the model class for your module app\code\local\Company\Web\Model\Web.php
<?php

class Company_Web_Model_Web extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('web/web');
    }
}

Step 10: Now create app\code\local\Company\Web\Model\Mysql4\Web.php
<?php
class Company_Web_Model_Mysql4_Web extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {    
        // Note that the web_id refers to the key field in your database table.
        $this->_init('web/web', 'web_id');
    }
}

Step 11: Now create the collection class app\code\local\Company\Web\Model\Mysql4\Web\Collection.php
<?php

class Company_Web_Model_Mysql4_Web_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('web/web');
    }
}

Step 12: Now add the mysql setup file as app\code\local\Company\Web\sql\web_setup\mysql4-install-0.1.0.php
<?php

$installer = $this;

$installer->startSetup();

$installer->run("

-- DROP TABLE IF EXISTS {$this->getTable('web')};
CREATE TABLE {$this->getTable('web')} (
  `web_id` int(11) unsigned NOT NULL auto_increment,
  `title` varchar(255) NOT NULL default '',
  `filename` varchar(255) NOT NULL default '',
  `content` text NOT NULL default '',
  `status` smallint(6) NOT NULL default '0',
  `created_time` datetime NULL,
  `update_time` datetime NULL,
  PRIMARY KEY (`web_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    ");

$installer->endSetup(); 

Step 13: Add the layout.xml as app\design\frontend\default\default\layout\web.xml
<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
    </default>
    <web_index_index>
        <reference name="content">
            <block type="web/web" name="web" template="web/web.phtml" />
        </reference>
    </web_index_index>
</layout> 

Step 14: Finally create the template file of your module app\design\frontend\default\default\template\web\web.phtml
<?php
 echo "Welcome to your custom module....";
?>

3 comments: