featured image

Develop “Hello world” Magento extension

posted on Monday April 27th, 2015 / IN Uncategorized /

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. <config>
  2.     <modules>
  3.         <bb_product>
  4.             <version>1.0>
  5.         >
  6.     >
  7. >

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. <config>
  2.     <modules>
  3.         <Bb_Product>
  4.             <active>true>
  5.             <codePool>community>
  6.         >
  7.     >
  8. >

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

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

config.xml
  1. <config>
  2.     <modules>
  3.         <Bb_Product>
  4.             <version>1.0>
  5.         >
  6.     >
  7.     <frontend>
  8.         <routers>
  9.                 <bbproduct>
  10.                         <use>standard>
  11.                         <args>
  12.                                 <module>Bb_Product>
  13.                                 <frontName>bbproduct>
  14.                         >
  15.                 >
  16.         >
  17.     >
  18. >

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.
    By Truong Hua

    You are reading a post from a man who have passionate interest in information technology :). Details about me are here https://linkedin.com/in/truonghua/ and I'm willing to connect with passionate and motivated people.