This is one of my first article in the series of How to’s in magento. But before you actually start any development on magento you must follow these guidelines.
Step 1: First create a magento module xml file
( app/etc/modules/Pw_Manageproducts.xml)
[break][/break]1 2 3 4 5 6 7 8 9 | <?xml version="1.0"?> <config> <modules> <Pw_Manageproducts> <active>true</active> <codePool>local</codePool> </Pw_Manageproducts> </modules> </config> |
This xml tells magento that module is active and will be available in local codePool.
At this point, you should be able to see your module in Magento Admin Panel. Go to System->Configuration->Advanced->Disable Modules Output. You should be able to see Pw_Manageproducts module listed there.
Step 2: Create your magento module’s directory structure
[break][/break]app/code/local/Pw/Manageproducts/ –Block/ –controllers/ –etc/ –Helper/ –sql/ |
You wont really need all above folders all the time but we will create them all.
Naming folders is very important, to be consistent, keep First letter capital and all others in small.
[break][/break]
Step 3: Create a Block Class
(app\code\local\Pw\Manageproducts\Block\Manageproducts.php)
[break][/break]1 2 3 4 5 | <?php class Pw_Manageproducts_Block_Manageproducts extends Mage_Core_Block_Template { } ?> |
You can create multiple blocks or even override an existing core magento block. We will discuss separately how you can override a block. However note the word you put after Block_ i.e. Manageproducts is going to be your block name which we will use in future.
[break][/break]Step 4: Write the front controller for your module
(app\code\local\Pw\Manageproducts\controllers\IndexController.php)
[break][/break]You dont really need a controller for a backend module, If you want to call your module via a url then your can define an IndexController ?
1 2 3 4 5 6 7 8 9 10 | <?php class Pw_Manageproducts_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { $this->loadLayout(); $this->renderLayout(); } } ?> |
Step 5: Define a config.xml file
(app\code\local\Pw\Manageproducts\etc\config.xml).
[break][/break]1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Pw_Manageproducts> <version>1.0.1</version> </Pw_Manageproducts> </modules> <global> <models/> <blocks> <manageproducts> <class>Pw_Manageproducts_Block</class> </manageproducts> </blocks> </global> <frontend> <routers> <manageproducts> <use>standard</use> <args> <module>Pw_Manageproducts</module> <frontName>manageproducts</frontName> </args> </manageproducts> </routers> <layout> <updates> <manageproducts> <file>pw/manageproducts.xml</file> </manageproducts> </updates> </layout> </frontend> <default> <manageproducts> <product> </product> </manageproducts> </default> </config> |
Note: We will discuss what each configuration means in detail later, Although they are pretty straightforward. We have define one frontend tag, this is necessary only if you are planning to use it in frontend. router is needed when you are going to call this as a external link(for ex: www.example.com/index.php/manageproducts can be called). layout is used to configure layout options for ex: which template to use, are we plugging it before/after some existing block output(will explain this as well with some examples later).
[break][/break]Step 6: Now create a template file, which is used to render html.
(app\design\frontend\default\default\template\pw\manageproducts\manageproducts.phtml)
[break][/break]1 2 3 | <?php echo "<h1>We are going to manage our magento products...</h1>"; ?> |
In templates and layouts, it is a good practice to use all small letters for file and folder name (ex: pw/manageproducts), stick with it to save your time debugging.
[break][/break]Step 7: Create a layout.xml file
(app\design\frontend\default\default\layout\pw\manageproducts.xml)
[break][/break]path for this file is defined in config.xml file, you can change this location as per your desire.
1 2 3 4 5 6 7 8 9 10 11 | <?xml version="1.0"?> <layout version="0.1.0"> <manageproducts_index_index> <reference name="root"> <action method="setTemplate"><template>page/1column.phtml</template></action> </reference> <reference name="content"> <block type="manageproducts/manageproducts" name="manageproducts" template="pw/manageproducts/manageproducts.phtml"/> </reference> </manageproducts_index_index> </layout> |
Step 8: How to use the plugin.
[break][/break]Our new magento module is ready to be used, but wait ! How to use it ? I have listed few easy ways to use it below.
- Via a URL: www.example.com/index.php/manageproducts (replace manageproducts with the frontName defined in config.xml)
- In CMS Content Editor: (You dont need step7 for this)
{{block type="manageproducts/manageproducts" template="pw/manageproducts/manageproducts.phtml"}}
here in block type, first manageproducts is the module name given in config.xml sencond manageproducts is the block name in step 3.
- In a PHP file:
<?php echo $this->getLayout()->createBlock('manageproducts/manageproducts') ->setBlockId('manageproducts')->toHtml() ; ?>
vivek says
sir i follow your tutorial steps but when enter the url this page give me a blank page… why any suggestion