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

Code pools

The folder code is a placeholder for what is called a codePool in Magento. Usually, there are three code pools in Magento, that is, three subfolders: community, core, and local.

The folder local is sometimes missing from the downloaded installation archive, as it is empty by default.

Let's take a deeper look at the community codePool for the default Magento installation as shown in the following diagram:

└── Phoenix
    └── Moneybookers
        ├── Block
        │   ├── Form.php
        │   ├── ...
        │   └── Redirect.php
        ├── Helper
        │   └── Data.php
        ├── Model
        │   ├── Abstract.php
        │   ├── ...
        │   └── Wlt.php
        ├── controllers
        │   ├── MoneybookersController.php
        │   └── ProcessingController.php
        ├── etc
        │   ├── config.xml
        │   └── system.xml
        └── sql
            └── moneybookers_setup
                ├── install-
                └── mysql4-upgrade-1.2-

Here, the Phoenix folder is what is called the vendor namespace, and it usually matches your company identifier or something else unique to you. Within the Phoenix folder there is a Moneybookers subfolder that stands for your actual extension name.

To summarize, the formula for your extension code location should be something like app/code/community/YourNamespace/YourModuleName/ or app/code/local/YourNamespace/YourModuleName/.

There is a simple rule as to whether to choose community or local codePool:

  • Choose the community codePool for extensions that you plan to share across projects, or possibly upload to Magento Connect

  • Choose the local codePool for extensions that are specific for the project you are working on and won't be shared with the public

For example, let's imagine that our company name is Foggyline and the extension we are building is called Happy Hour . As we wish to share our extension with the community, we can put it into a folder such as app/code/community/Foggyline/HappyHour/.

All the Magento core code is also divided into extensions, and is located under the app/code/core/Mage folder. You should never place any of your code or edit any of the existing code under the app/code/core folder.

Let us get back to our example from the previous listing, the Moneybookers extension. We can see that it has several subfolders within it:

  • Block: This folder contains various PHP classes. You can think of the Block folder as a placeholder for class objects that visually manifest themselves to the user on a frontend. Most of these PHP classes extend the Mage_Core_Block_Template class from within the app/code/core/Mage/Core/Block/Template.php file. These PHP classes are then linked to various layouts and template *.phtml files within the given theme under the app/design folder.

  • controllers: This folder contains various PHP classes. You can think of controllers as a glue between our URL actions, models, blocks, and views. Most of these classes extend the Mage_Core_Controller_Front_Action class from within the app/code/core/Mage/Core/Controller/Front/Action.php file or the Mage_Adminhtml_Controller_Action class from within the app/code/core/Mage/Adminhtml/Controller/Action.php file.

  • etc: This folder contains various XML configuration files such as adminhtml.xml, api.xml, config.xml, system.xml, wsdl.xml, wsdl2.xml, and wsi.xml. Depending on what type of extension you are building, you might find some configuration files used more than the others.

  • Helper: This folder contains various PHP classes, most of which extend the Mage_Core_Helper_Abstract class from within the app/code/core/Mage/Core/Helper/Abstract.php file. The Helper classes contain various utility methods that will allow you to perform common tasks.

  • Model: This folder contains various PHP classes that usually, but not necessarily, represent an entity in a database. This is the folder where you would place most of your business logic.

  • sql: This folder contains one or more PHP files representing the installer code to be executed during the installation of the extension.

With that said, we will temporarily conclude our trip to the app/code folder structure and move on to the app/etc/modules folder.

This folder is basically a starting point for every Magento extension. The following listing shows the default content of the app/etc/modules folder for the default Magento installation, which is a collection of XML files:

  • Mage_All.xml

  • Mage_Downloadable.xml

  • Mage_Api.xml

  • Mage_ImportExport.xml

  • Mage_Api2.xml

  • Mage_Oauth.xml

  • Mage_Authorizenet.xml

  • Mage_PageCache.xml

  • Mage_Bundle.xml

  • Mage_Persistent.xml

  • Mage_Captcha.xml

  • Mage_Weee.xml

  • Mage_Centinel.xml

  • Mage_Widget.xml

  • Mage_Compiler.xml

  • Mage_XmlConnect.xml

  • Mage_Connect.xml

  • Phoenix_Moneybookers.xml

  • Mage_CurrencySymbol.xml

For example, if we were to create our Foggyline/Happy Hour extension, we would need to create a file app/etc/modules/Foggyline_HappyHour.xml as we will show later on.

Next, we move onto the app/local folder. This is where the translation files reside. If you were building an extension that would support multiple languages, for example English and German, you might want to create the following files:

  • app/locale/en_US/Foggyline_HappyHour.csv

  • app/locale/de_DE/Foggyline_HappyHour.csv

The exact filename in this case does not have to be Foggyline_HappyHour.csv; this is something that is set by you within the extension configuration.