Develop "Hello world" Magento extension

To develop magento extension, first of all, we must understand about Magento Folder Structure. It you are doing some update but no changing, try to disable Magento cache at System/Cache Management.
All magento extension should be place in /app/code/community. In /app/code you also see core folder, sometime may be local folder which is created by other extension. The community or local called Code Pool.

Begining, we create our extension folder structure, if my company named Bb, and my extension name is Product, I will create the folder structure /app/code/community/Bb/Product. It's mean our extension stay in community Code Pool and our extension is Bb_Product.

Store your extension information in /app/code/community/Bb/Product/etc/config.xml. The simple extension config file may be follow:

config.xml
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <bb_product>
  5.             <version>1.0</version>
  6.         </bb_product>
  7.     </modules>
  8. </config>
Hide/show line number

Now, our extension has it own profile, to let Magento load and control your extension, you must tell it "Heyx, load my extension please, my extension is Bb_Product in community Code Pool!", so just create /app/code/etc/modules/Bb_Product.xml file:

Bb_Product.xml
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <Bb_Product>
  5.             <active>true</active>
  6.             <codePool>community</codePool>
  7.         </Bb_Product>
  8.     </modules>
  9. </config>
Hide/show line number

That enough, our extension now can be loaded by Magento and you can enable or disable our extension in Admin Control Panel at System/Configuration/Advanced.

The next mission is create a page that output "Hello world". Magento is base on Zend, so Magento is MVC base. If you don't know anything about MVC, you should try to understand it first!

Create IndexControler.php file in /app/code/community/Bb/Product/controllers (offcourse, create /app/code/community/Bb/Product/controllers first)

IndexController.php
  1. <?php
  2. class Bb_Product_IndexController extends Mage_Core_Controller_Front_Action {
  3.        
  4.         public function indexAction() {
  5.                 echo 'Hello world';
  6.         }
  7.        
  8.         public function myactionAction() {
  9.                 echo 'Hello world, this is myaction';
  10.         }
  11.        
  12.         public function myaction2Action() {
  13.                 $this->loadLayout();
  14.                 $this->renderLayout();
  15.         }
  16. }
  17. ?>
Hide/show line number

Tell Magento know when should it use our controller, append some code into /app/code/community/Bb/Product/etc/config.xml.

config.xml
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <Bb_Product>
  5.             <version>1.0</version>
  6.         </Bb_Product>
  7.     </modules>
  8.     <frontend>
  9.         <routers>
  10.                 <bbproduct> <!-- this is url path we use -->
  11.                         <use>standard</use>
  12.                         <args>
  13.                                 <module>Bb_Product</module> <!-- this is our extension name -->
  14.                                 <frontName>bbproduct</frontName>
  15.                         </args>
  16.                 </bbproduct>
  17.         </routers>
  18.     </frontend>
  19. </config>
Hide/show line number

You have already created Hello world magento extension. Visit our extension at:

  • http://yourdomain.com/index.php/bbproduct/index/index (it is the same with http://yourdomain.com/index.php/bbproduct/index or http://yourdomain.com/index.php/bbproduct).
    You should see "Hello world" text.
  • http://yourdomain.com/index.php/bbproduct/index/myaction.
    This output "Hello world, this is myaction"
  • http://yourdomain.com/index.php/bbproduct/index/myaction2
    This action will output default Magento page template.