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
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Controllers


By itself, the content of the app/code/community/Foggyline/HappyHour/etc/config.xml file won't have any additional effect on Magento, so let's move on to extending our extension. First, we will create a controller in order to output Hello World to the browser. To do this, we need to add the routers definition in frontend to our config.xml file.

<?xml version="1.0"?>"?>
<config>
<!-- … other elements ... -->
   <frontend>
      <routers>
         <foggyline_happyhour>
            <use>standard</use>
               <args>
                  <module>Foggyline_HappyHour</module>
                  <frontName>happyhour</frontName>
               </args>
         </foggyline_happyhour>
      </routers>
   </frontend>
<!-- … other elements ... -->
</config>

The frontend tag refers to a Magento area. Magento has three distinctive areas: frontend, admin, and install. The frontend area is what your customers see, the public facing shopping cart. The admin area is what your Magento admin users see, the administrative interface. The install area is what you see the very first time you install Magento, the installation process.

The routers tag encloses the configuration information about routers.

The frontName tag is sort of an alias for the desired route we want Magento to react to.

When a router parses a URL, it gets separated as follows: http://example.com/frontName/actionControllerName/actionMethod/. By defining a value of happyhour in the <frontName> tag, we're telling Magento that we want the system to respond to URLs in the form of http://example.com/happyhour/*.

It's important to understand that frontName and the Front Controller object are not the same thing.

The foggyline_happyhour tag should be the lowercase version of your extension name. Our extension name is Foggyline_HappyHour; this tag is foggyline_happyhour.

The extension tag should be the full name of your extension, including its namespace/extensionname name. This will be used by the system to locate your controller files.

Now we need to create a controller file. The module controller files are stored under the controllers subfolder. So let's create an app/code/community/Foggyline/HappyHour/controllers/HelloController.php class file with the following content:

<?php

class Foggyline_HappyHour_HelloController extends Mage_Core_Controller_Front_Action
{
   public function helloWorldAction()
   {
      echo 'Hello World #1.';
   }
}

Once you are done, you can try opening the following URL in the browser: http://magento.loc/index.php/happyhour/hello/helloWorld. You should be able to see the Hello World #1. message. The URL path is constructed from your config.xml router frontName, the controller name itself, and the controller action name. There are two main types of controllers in Magento:

  • frontend: This contains all the controller classes that extend (derive from) the Mage_Core_Controller_Front_Action class

  • backend / admin: This contains all the controller classes that extend (derive from) the Mage_Adminhtml_Controller_Action class

URLs for admin controller actions can only be accessed if you are logged in to the Magento administration interface.