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

The root directory structure


Once you download the full release and set up your work environment, you should see a root Magento folder structure with the following files and folders in it:

  • Folders : app, downloader, errors, includes, js, lib, media, pkginfo, shell, skin, and var

  • Files : .htaccess, cron.sh,.htaccess.sample, LICENSE.html, mage, LICENSE.txt, favicon.ico, LICENSE_AFL.txt, get.php php.ini.sample, RELEASE_NOTES.txt, api.php, index.php, index.php.sample, cron.php, and install.php

Throughout this book we will be referencing our URL examples as if they are executing on the magento.loc domain. You are free to set your local Apache virtual host and host file to any domain you prefer, as long as you keep this in mind. If you're hearing about virtual host terminology for the first time, please refer to the Apache Virtual Host documentation at http://httpd.apache.org/docs/2.4/vhosts/.

Here is a quick summary of each of those files and folders:

  • .htaccess: This file is a directory-level configuration file supported by several web servers, most notably the Apache web server. It controls mod_rewrite for fancy URLs and sets configuration server variables (such as memory limit) and PHP maximum execution time.

  • .htaccess.sample: This is basically a .htaccess template file used for creating new stores within subfolders.

  • api.php: This is primarily used for the Magento REST API, but can be used for SOAP and XML-RPC API server functionality as well.

  • app: This is where you will find Magento core code files for the backend and for the frontend. This folder is basically the heart of the Magento platform. Later on, we will dive into this folder for more details, given that this is the folder that you as an extension developer will spend most of your time on.

  • cron.php: This file, when triggered via URL or via console PHP, will trigger certain Magento cron jobs logic.

  • cron.sh: This file is a Unix shell script version of cron.php.

  • downloader: This folder is used by the Magento Connect Manager, which is the functionality you access from the Magento administration area by navigating to System | Magento Connect | Magento Connect Manager.

  • errors: This folder is a host for a slightly separate Magento functionality, the one that jumps in with error handling when your Magento store gets an exception during code execution.

  • favicon.ico: This is your standard 16 x 16 px website icon.

  • get.php: This file hosts a feature that allows core media files to be stored and served from the database. With the Database File Storage system in place, Magento would redirect requests for media files to get.php.

  • includes: This folder is used by the Mage_Compiler extension whose functionality can be accessed via Magento administration System | Tools | Compilation. The idea behind the Magento compiler feature is that you end up with a PHP system that pulls all of its classes from one folder, thus, giving it a massive performance boost.

  • index.php: This is a main entry point to your application, the main loader file for Magento, and the file that initializes everything. Every request for every Magento page goes through this file.

  • index.php.sample: This file is just a backup copy of the index.php file.

  • js: This folder holds the core Magento JavaScript libraries, such as Prototype, scriptaculous.js, ExtJS, and a few others, some of which are from Magento itself.

  • lib: This folder holds the core Magento PHP libraries, such as 3DSecure, Google Checkout, phpseclib, Zend, and a few others, some of which are from Magento itself.

  • LICENSE*: These are the Magento licence files in various formats (LICENSE_AFL.txt, LICENSE.html, and LICENSE.txt).

  • mage: This is a Magento Connect command-line tool. It allows you to add/remove channels, install and uninstall packages (extensions), and various other package-related tasks.

  • media: This folder contains all the media files, mostly just images from various products, categories, and CMS pages.

  • php.ini.sample: This file is a sample php.ini file for PHP CGI/FastCGI installations. Sample files are not actually used by the Magento application.

  • pkginfo: This folder contains text files that largely operate as debug files to inform us about changes when extensions are upgraded in any way.

  • RELEASE_NOTES.txt: This file contains the release notes and changes for various Magento versions, starting from version 1.4.0.0 and later.

  • shell: This folder contains several PHP-based shell tools, such as compiler, indexer, and logger.

  • skin: This folder contains various CSS and JavaScript files specific for individual Magento themes. Files in this folder and its subfolder go hand in hand with files in app/design folder, as these two locations actually result in one fully featured Magento theme or package.

  • var: This folder contains sessions, logs, reports, configuration cache, lock files for application processes, and possible various other files distributed among individual subfolders. During development, you can freely select all the subfolders and delete them, as Magento will recreate all of them on the next page request. From a standpoint of a Magento extension developer, you might find yourself looking into the var/log and var/report folders every now and then.

Now that we have covered the basic root folder structure, it's time to dig deeper into the most used folder of all, the app folder, as shown in the following diagram:

app/
├── Mage.php
├── code
│   ├── community
│   │   └── Phoenix
│   │       └── Moneybookers
│   └── core
│       ├── Mage
│       └── Zend
├── design
│   ├── adminhtml
│   ├── frontend
│   │   ├── base
│   │   │   └── default
│   │   │       ├── etc
│   │   │       ├── layout
│   │   │       └── template
│   │   └── default
│   │       ├── blank
│   │       ├── default
│   │       ├── iphone
│   │       └── modern
│   └── install
├── etc
│   ├── config.xml
│   ├── local.xml.additional
│   ├── local.xml.template
│   └── modules
└── local
    └── en_US