Book Image

Getting Started with Magento Extension Development

By : Branko Ajzele
Book Image

Getting Started with Magento Extension Development

By: Branko Ajzele

Overview of this book

Modules, are a group of php and xml files meant to extend the system with new functionality, or override core system behavior. Most of the base Magento system is built using the module system, so you can see why they are an important feature for this rich open-source e-commerce solutions. This book explores key module development techniques and teaches you to modify, understand and structure your modules making it easy for you to get a strong foundation for clean and unobtrusive Magento module development. Getting Started with Magento Extension Development is a practical, hands-on guide to building Magento modules from scratch. This book provides an in depth introduction and helps you discover features such as; blocks, controllers, models, configuration files, and other crucial elements which contribute to the Magento architecture. This book introduces the you to real-world modules and helps provide a strong foundation which you need to become a professional Magento module developer. The book further explores best practices and tips and tricks offering you the ultimate go to guide. Getting Started with Magento Extension Development focuses on three areas. First you are guided through the entire Magento structure, where each important directory or file is explored in detail. Then the essence of the module structure and development is explained through the detailed coverage of models, blocks, controllers, configuration, and other files that manifest a single module. Finally, a detailed set of instructions is given for building four real-world modules, including a payment and shipping module.
Table of Contents (13 chapters)
Getting Started with Magento Extension Development
About the Author
About the Reviewers


Looking within the helloWorldAction() method, you can see a call towards the createBlock() method with the string 'core/text' as a parameter. Where does 'core/text' come from and what does it mean? In order to understand that, we will further extend our config.xml file by adding a blocks element to it as follows:

<?xml version="1.0"?>
<!-- … other elements ... -->
<!-- … other elements ... -->

The element foggyline_happyhour is known as the class group. The element foggyline_happyhour is a class group name and its inner class element is basically a shortcut for your extensions Block type PHP classes. For example, the following is a modified code for our helloWorldAction() method shown previously:


class Foggyline_HappyHour_HelloController extends Mage_Core_Controller_Front_Action
   public function helloWorldAction()
      $block = $this->getLayout()->createBlock('foggyline_happyhour/hello');
      $block->setText('Hello World #2.');

You can see that we are no longer calling the createBlock() method with 'core/text' but with the 'foggyline_happyhour/hello' parameter. This is like telling Magento to load the hello class (the Hello.php file) that can be found under the classpath mapped by the foggyline_happyhour class group. As the foggyline_happyhour class group has its class value set to Foggyline_HappyHour_Block, Magento expects to find the app/code/community/Foggyline/HappyHour/Block/Hello.php file.

How and why exactly does Magento expect the Hello.php file to be at a certain location? The answer to this lies in a robust autoloading functionality of the Magento system based on a configuration and file naming convention. You can split all Magento classes into four parts that we'll call the vendor namespace, extension name, class group, and filename itself.

The vendor namespace helps us prevent name collisions between extensions, letting you know which extension is the owner of the class. For example, all core Magento extensions use the mage namespace.

The module name plays a crucial part in the autoloading system. All the proper customization of Magento is done through individual extensions.

The class group is a sort of alias defined within the extension's configuration file, an alias towards a class folder within the extension directory. There are several main types of class groups, such as the one for Model, Block, Helper.

Finally, the name of the file itself. Each class should have a unique name within a class group that describes its intended use or function.

Magento's autoloading functionality uses these parts to determine where to find the source for a given class as shown in the following example: VendorNamespace/ModuleName/ClassGroup/FileName.php.

Go ahead and create the file Hello.php with the following content:


class Foggyline_HappyHour_Block_Hello extends Mage_Core_Block_Text

Now that you have modified helloWorldAction() and created the Hello.php class file, go ahead and open the http://magento.loc/index.php/happyhour/hello/helloWorld URL in the browser. The result should be the same as in the previous case; you should be able to see the fully loaded Magento page with the Hello World #2. message shown under the content area.

Our Hello block class extends Mage_Core_Block_Text. However, chances are that most of the time you will be extending the Mage_Core_Block_Template class, where your Hello block class might look like something as follows:


class Foggyline_HappyHour_Block_Hello extends Mage_Core_Block_Template
   public function __construct()

The difference between extending Mage_Core_Block_Text or Mage_Core_Block_Template is that the latter requires you to define a view *.phtml file under the theme folder, and thus, is more designer friendly. In order for it to successfully work, you need to create the app/design/frontend/default/default/template/foggyline_happyhour/hello.phtml or app/design/frontend/base/default/template/foggyline_happyhour/hello.phtml file. You might find the latter to be a safer location for your view files, as it is not dependent on your customer theme and package settings. Thus, the Magento theme fallback mechanism will always pick it up. Now if you put your Hello World #3. string within the hello.phtml file and then re-open the http://magento.loc/index.php/happyhour/hello/helloWorld URI in your browser, you should again see the fully loaded Magento page with the Hello World #3. message shown under the content area. Our goal here is to give you the basics of functional extension, so we will now leave the controllers and blocks behind and move to the model.


Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.