featured image

Develop “Hello world” Magento extension

posted on 27/04/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. version=“1.0”?>
  2. >
  3.     >
  4.         >
  5.             >1.0>
  6.         >
  7.     >
  8. >

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. version=“1.0”?>
  2. >
  3.     >
  4.         >
  5.             >true>
  6.             >community>
  7.         >
  8.     >
  9. >

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. class Bb_Product_IndexController extends Mage_Core_Controller_Front_Action {
  2.         public function indexAction() {
  3.                 echo ‘Hello world’;
  4.         }
  5.         public function myactionAction() {
  6.                 echo ‘Hello world, this is myaction’;
  7.         }
  8.         public function myaction2Action() {
  9.                 $this->loadLayout();
  10.                 $this->renderLayout();
  11.         }
  12. }
  13. ?>

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. version=“1.0”?>
  2. >
  3.     >
  4.         >
  5.             >1.0>
  6.         >
  7.     >
  8.     >
  9.         >
  10.                 >
  11.                         >standard>
  12.                         >
  13.                                 >Bb_Product>
  14.                                 >bbproduct>
  15.                         >
  16.                 >
  17.         >
  18.     >
  19. >

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.